From Chris Denham, "I discovered what seems to be a missing call to FreeLibrary? for osgDB.dll The result of this is that it prevents me from dynamically unloading my DLL (which uses OSG). This is because initialisation of the osg::Registry instance results in a call to GetModuleHandleEx? (to get the filename of the DLL). This call to GetModuleHandleEx? increments the module refcount, but there was no matching call to FreeLibrary? for the returned handle. Unlike the old GetModuleHandle? function, GetModuleHandle? does (by default) increment the refcount.
I have added the missing call to FreeLibrary? in osgDB::FileUtils?., and now my the runtime unload of MY dll is working properly. It has also cured some related problems I was having with memory leak checks being reported.
I have attached a fix to osgDB/FileUtils.cpp based on version 2.9.5 svn revision 10374