DLL memory initialization/release

DLL memory initialization/release

A 40,000 line legacy Fortran application is built as a DLL to be called from Visual Basic.

I suspect a particular runtime error is caused the by code's failure to properly initialize some local variable inside the DLL. The first time the DLL is executed, everything is fine. But if the DLL is hit a second time during one execution of the VB appplication, problems occur.

Is there a VB-callable function to release the Fortran DLL? Any suggestions are greatly appreciated.

I am speculating that such a function would resolve my problem, since the variables would be reinitialized on re-load.

4 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

Such a routine exists - the Win32 API routine FreeLibrary. To use it, you need to get the "handle" of the loaded DLL. VB won't give it to you, but you can get it by calling GetModuleHandle with the DLL name. The catch may be that VB will go off the deep end if you try this because it will think that the DLL is already loaded and will get confused the next time you call the DLL. Try it and see....


Retired 12/31/2016

...of course, that's a cluge -- it's still better to fix the offending code. Try compiling with "Variables default to automatic" switch -- that will probably cause havoc to code's behaviour since you'll (probably) get garbage values for all non-unitialized... the rest is pure debugging ;-).



Supply a DllMain for your Fortran DLL and see if the fwdReason parameter returns DLL_ATTACH_NOTIFICATION the second time you hit the DLL. My guess I that it doesn't so the DLL isn't being reinitialized from one hit to the next. The original Fortran code was undoubtedly written to run in batch mode and its creators are to be forgiven for not anticipating that one day someone would compile it as a DLL and run serial or threaded instances of it without ever giving it a break. If you load the DLL in VB via ModuleID = LoadLibrary("MyCVF.DLL"), use it, and then unload it via FreeLibrary(ModuleID), and repeat all of this for subsequent hits, your DLL will be reinitialized from hit to hit and DllMain should fire a DLL_ATTACH_NOTIFICATION each time you hit your initialized DLL.

Leave a Comment

Please sign in to add a comment. Not a member? Join today