Object Oriented DLL Problem

Started by
1 comment, last by PsyCHo puNk 22 years, 9 months ago
Hi I made a post about this problem a while back but still haven''t found a solution so I''m here with a much better explaination of whats wrong. I am trying to package a reusable class library in a win32 dll however I have run into a problem, Class B and B2 derive from class A, heres the simplified code: class __declspec( dllexport ) A { public: A(); virtual ~A(); virtual void SomeFunc(void) = 0; }; class __declspec( dllexport ) B : public A { public: B(); virtual ~B(); void SomeFunc(void); }; class __declspec( dllexport ) B2 : public A { public: B2(); virtual ~B2(); void SomeFunc(void); }; A::A() { cout << "A constructor." << endl; } A::~A() { cout << "A destructor." << endl; } B::B() { cout << "B constructor." << endl; } B::~B() { cout << "B destructor." << endl; } void B::SomeFunc(void) { cout << "B::SomeFunc()" << endl; } B2::B2() { cout << "B2 constructor." << endl; } B2::~B2() { cout << "B2 destructor." << endl; } void B2::SomeFunc(void) { cout << "B2::SomeFunc()" << endl; } This compiles into a DLL without any problems. However when I attempt to use it (I have linked to the .lib and put the .dll in the executable directory) using the following code: #include "A.h" #include "B.h" #include "B2.h" int main(void) { A* test = NULL; test = new B(); test->SomeFunc(); delete test; return 0; } I get the output as expected: A constructor. B constructor. B::SomeFunc() B destructor. A destructor. But then a nasty debug assertion failure: Debug Asserion Failed. File: dbgheap.c Expression: _pFirstBlock == pHead Oddly enough, if I comment out delete test; the assertion failure does not occur, but I dont want memory leaks in my programs. I know that converting it too a COM dll and using interfaces would problably be the best solution, however I want to first learn how to create normal dlls before I mess with making them COM compliant.
Advertisement
This is a long shot but lets try it any way:
When ever I use:__declspec( dllexport ) to export a class I also use __declspec( dllimport ) to import it. the way to do it is:
in the class definition write something like this:

#ifdef _Win32DLLTrgt
#define EXPORT_FLAG __declspec( dllexport )
#else
#define EXPORT_FLAG __declspec( dllimport )
#endif

class EXPORT_FLAG A
{
A();
virtual ~A();
}

if the project that creates the dll add to the setting -D_Win32DLLTrgt (you can do it is the setting with out the /D option )
Hope this is clear
-- Gilad
My best guess is that problem here is that the main application and the dll are using separate heaps. Chances are in your class it is allocating some memory on the dll heap, but attempting to free it on the main application heap, or vice version.

There are two main ways to tackle this:

1) Change the run time library to be Debug Multithreaded DLL and Multithreaded DLL, for debug and release mode respetively.

2) Or, add in a detroy() or deleteThis() to a common base class, which simply calls "delete this;" so it ensures the dll cleans up its memory.

Option 1 would be my best bet, as you wont have to make sure that memory newed in one place is deleted in the same place.

Phillip Martin

This topic is closed to new replies.

Advertisement