A program I wrote (in C#) accesses through a C++/CLI wrapper library a native (c++) library I also wrote.
I noticed that the native library’s DllMain method is called more than once, and the debugger thinks that multiple memory locations are connected to some of the library calls. From what I see, the library is initialized (and memory allocated) more than once.
Given that my code does not use functions such as LoadLibrary, and the dll is used including the library’s .h files and .lib, why is it initialized more than once, and what can I do to avoid it?
AFAIK DLL may not be loaded more than once into the same address space. Even if you call
LoadLibraryexplicitly the DLL won’t be loaded more than once, instead its reference counter is incremented.Also I don’t understand exactly what you mean by “debugger thinks that multiple memory locations are connected to some of the library calls“. A single imported symbol (function or a variable address) is filled by just one address during DLL binding.
I believe you have one of the following:
DllMainis called several times, but it does not mean it’s loaded several times. Do you check the parameters supplied to theDllMain? Is itDLL_PROCESS_ATTACH, or perhaps it’s justDLL_THREAD_ATTACH/DLL_THREAD_DETACH?