Jump to content

  • Log In with Google      Sign In   
  • Create Account

Object Oriented DLL Problem


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 PsyCHo puNk   Members   -  Reputation: 122

Like
Likes
Like

Posted 21 July 2001 - 08:21 PM

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.

Sponsor:

#2 gilady   Members   -  Reputation: 122

Like
Likes
Like

Posted 22 July 2001 - 12:42 AM

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

#3 Phillip Martin   Members   -  Reputation: 226

Like
Likes
Like

Posted 22 July 2001 - 03:37 PM

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





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS