4

My architecture is as follows:

  • Program.exe links to 1.dll
  • 1.dll attempts to load DLL files in some directory (i.e. 2.dll) via LoadLibrary
  • 1.dll attempts to find a function called "setup" via GetProcAddress("setup")
  • 1.dll does some work and may release 2.dll
  • The user may want to change the DLLs in the directory, including removing a DLL (without restarting the software)

The issue I'm having is that 2.dll in my example remains locked (error 32), even after FreeLibrary has been called.

In my initial testing Program.exe could LoadLibrary, do something with it and FreeLibrary, which unlocked it, then the same functionality has been transfered into 1.dll, which Program.exe also loaded via LoadLibrary, and the process continued to work perfectly.

In order to avoid dynamically loading 1.dll I linked it with Program.exe, which seems to have broken it, as aside from changing the linking nothing else has changed, and the dlls loaded by 1.dll remain locked until the program is terminated, FreeLibrary still returns true.

As there are a multitude of functions that I would need to dynamically load, which seems rather unrealistic I'd like to fix the issue with other dlls being locked. How can I make the program release unused files, like it did before I linked 1.dll with it?

user_4685247
  • 2,878
  • 2
  • 17
  • 43
  • 2
    Are you sure you called *FreeLibrary* on *2.dll*? Also are you sure you didn't call *LoadLibrary* on it more than once? – CristiFati Jan 11 '21 at 15:33
  • Yes, if I load 1.dll via LoadLibrary everything works fine, linking the same DLL to the exe will hold a reference to the file. Neither 1.dll nor 2.dll have been recompiled. – user_4685247 Jan 11 '21 at 16:16
  • _"The user may want to change the DLLs in the directory, including removing a DLL (without restarting the software"_. I do not think this is feasible. A library is loaded at run-time based on build and compile time constraints. removing a part of the process (of which the logical content of the .dll has contributed) after it is started I think would be a problem. – ryyker Jan 11 '21 at 17:22
  • 1
    You really should consider add a [mcve] to show details related to this problem. I am not sure how anyone can draw conclusions on a solution without first seeing what the code is doing. – ryyker Jan 11 '21 at 17:25
  • I'll attempt to extract a minimal example. As for removing the DLL, I have managed to use FreeLibrary to do just that, in my case it just stops working when the exe links to the library that does the loading. – user_4685247 Jan 11 '21 at 20:56
  • *Btw*, what did you use to build the code? – CristiFati Jan 12 '21 at 09:06
  • the code was built with CMAKE build system, mingw32 compiler. – user_4685247 Jan 12 '21 at 10:09
  • Not strictly related to question, but using *MinGW* (*posix* port for *Win*) with *WinAPI*s seems a bit strange. – CristiFati Jan 12 '21 at 10:45
  • If you link to the dll directly, you will not be able to unload it while the executable is running. That's only possible if it was loaded with LoadLibrary. – ssbssa Jan 12 '21 at 11:15
  • @ssbssa I only linked 1.dll, which was loading 2.dll, 2.dll was not linked. – user_4685247 Jan 12 '21 at 15:01
  • I know this is an older question, but I had the exact same issue myself. The solution was so weird, I fell I should at least mention it. All of my build artifacts were in a folder named 'release'. Renaming that folder from 'release' to ANYTHING ELSE behaves as expected. I can break and fix the issue trivially. Don't let 'release' confuse the issue; it is merely a folder name in my build process, and does not set any build flags. I am on Win10 using built-in terminal (Powershell) in VSCode to run .bat build scripts. I use 'mkdir' to create the target folder. Artifacts are built via clang. – christopherdrum Aug 18 '23 at 22:34
  • I'm going to follow this up with one more comment. As I noted above, I'm using clang. It seems that failing to specify -fuse-ld=lld (using the LLVM linker) caused my problems. I don't really understand how that manifested itself in the "release" folder issue, or the locked DLL issue to begin with. A new problem popped up today running an .exe (no rebuild) that was working perfectly yesterday. All three problems seem to have been resolved by specifying the lld linker. – christopherdrum Aug 19 '23 at 06:48

0 Answers0