views:

50

answers:

2

Hi, my problems deals with native C++ DLLs (Visual Studio 2005, if it matters) and how to write them in order to insure that:

  • when the DLL is compiled in release mode, it will be correctly loaded by an EXE compiled in release or debug mode (first priority)
  • when the DLL is compiled in debug mode, it will be correctly loaded by an EXE compiled in debug mode too.

Today I have a C++ native DLL that loads and works fine in DLL-release/EXE-release mode. The DLL loads but doesn't work fine (function calls return unexpected results) in DLL-release/EXE-release mode (and this is a huge problem, because it prevents me from debugging the EXE, which is my main goal) and crashes on a heap corruption in DLL-debug/EXE-debug mode.

I know that there is a CRT-related problem that requires CRT-isolation between DLL and EXE. Normally this problem is solved by making operators new/new[]/delete/delete[] private in the DLL and wrapping them by create()/release() functions that allows to the EXE dynamic object creation.

My question is: before I start re-factoring all my code in that direction, is there something else I need to do in order to avoid these kind of problems ? CRT-isolation will probably fix my DLL-debug/EXE-debug crash, but I'm not sure it will fix the DLL-release/EXE-debug problem.

Any hint ? Anyone having already been confronted to this problem ?

Thanks, Al.

A: 

Don't use configuration-specific code in public Dll h-files. For example:

class ExportedClass
{
...

#ifdef _DEBIG
    int debug_info;
#endif

...
}

When Dll is compiled in Debug configuration, sizeof(ExportedClass) contains additional 4 bytes. When this file is compiled by client code is Release configuration, sizeof(ExportedClass) is 4 bytes less. Result is undefined.

Also, don't use any types which have configuration-specific size, as part of Dll public interface: return values and parameters.

Alex Farber
Hi, thanks for your reply.Yes, I know, it is already done. For example, I carefully avoid using std in public DLL .h files because of that problem. My DLL code is quite self-contained and only uses either primitive types or types that are defined in DLL itself and whose size is not configuration dependent.
Alberto Avanzi
A: 

I would first and foremost solve the debug/debug scenario. The heap corruption is an indicator of a serious existing problem, and until not solved, you have no guarantee that any other scenario will work properly. in fact, I would argue that any other scenarios will not work properly either.

Also, I am not sure why would you think that the heap corruption needs to be solved by CRT isolation and is not a problem in the dll or exe code. Are the dll and the exe linked against different versions of the CRT?

Franci Penov
Hi, thanks for your reply. I'm almost sure that there is no heap corruption problems in the exe nor in the dll. Both of them have been working in release/release configuration for months under different OSs and PCs without any problem. Moreover, the crash happens as soon as a DLL-defined object is destroyed (delete operator). The call is perfectly legal, and that makes me to think that it handles of a CRT-related problem. Besides that, I agree with you that both my DLL and EXE are (in this moment) using the same version of CRT, so probably I should dig a bit more this heap corruption...
Alberto Avanzi