views:

56

answers:

1

I have a DLL that's written in C++Builder (2006), and I'm invoking a single function in the DLL from a .NET application.

The problem is, when I close the .NET application (and the DLL gets detached from it) I get a CodeGuard error saying that it detected resource leaks (and I see the leaks in the CodeGuard log file). I also see the LoaderLock MDA pop up in Visual Studio when freeing the DLL (probably for the same reason?).

Now, I'm 99% certain that I'm freeing all the memory I'm using in the DLL function. In fact, I tried building a dummy DLL with an identically-named function that does absolutely nothing, and still got resource leaks when the DLL is unloaded from the .NET application.

My only suspicion at this point is the fact that I'm using VCL built into the DLL. Is there some uninitialization function that I need to call explicitly when detaching from the DLL? What could be going on?

p.s. The memory addresses from the call stack given in the CodeGuard log file seem to be well beyond anything I see in my Map file.

+1  A: 

This sounds extremely familiar to an issue I chased for a while with Delphi. I asked the question on SO some while back. I tracked it down at least partially to threadvar (thread local storage) memory that is not freed. I believe that C++Builder uses the same VCL components as Delphi, so it could be the same issue. Unfortunately, I did not find any satisfactory solution other than to not unload the DLL if it was ever going to be loaded again in the future.

Mark Wilkins
Thanks for the response, but I'm not using any thread-local variables. I'm afraid this might be somewhere even deeper in Borland's RTL.
Dmitry Brant
@Dmitry: The VCL, however, does use thread local storage. So even if you don't use them explicitly, your DLL probably has them in it.
Mark Wilkins
Well, I tried implementing the code snippet you gave, but my TlsIndex (which is a global variable in VCL) always seems to be -1, unless I'm looking at the wrong thing.
Dmitry Brant
If SysInit.TlsIndex is -1, then it sounds like it is not using TLS storage in that module. So you are probably right; the problem must be something different from this.
Mark Wilkins