i have a dll that exports a class - it uses an abstract base class with pure virtual functions(defined in a header file), then a function to export it that returns a new object. insided the class is a 'release' function that deletes the object. now in another dll of mine, i dont do anything with the deconstructor, but in this one for some reason i have to have the same 'delete this' as i do in the release function or it crashes. now in the host application is where i get some sort of problem. note - it runs/finishes without error but some code isnt being exectued or something.
for the 1st reply: honestly i dont know why i needed it, i saw it done that way in a tutorial i read when learning how to export a class. and also the program crashes if i dont... 2nd reply - the release function is a class member in the class that's exported by CreateObject so you dont need to call getprocaddress for that. and i did check and it is getting fired correctly. here is a more complete code:
//#### header file for the dll
#define MYAPI __declspec(dllexport)
#define MYAPI __declspec(dllimport)
class IMyObj //abstract interface
//note - in sample tutorials ive read it says to have a deconstructor for the
// interface class but whenever i add 'virtual ~IMyObj()=0;' it doesnt compile.
virtual bool function1()=0;
virtual bool function2()=0;
virtual void release()=0;
//#####in dll(.cpp) file
class myObj : public IMyObj
cout << "deconstructor" << endl;
delete this; //as i said earlier in another app i wrote this line
//wasn't needed but now if i dont the program crashes
virtual bool function1()
virtual void release()
IMyObj* __stdcall CreateObject()
return new myObj;
//#### and then i include this header in the file that contains the code i previously posted.
sorry bout the tags guys. i totally agree that deleting it twice should cause an error. but when i remove one i get another crash. For the compilor I'm using Borland 5.5 command line tools (bcc32). the error -
(in ollydbg) - access violation when writing to -
so i removed the delete from the deconstructor and the 'delete obj' in the hosting application so now it is:
IMyObj = (pfnCreate)();
cout << "????" << endl;
//this last cout gets printed but the program crashes right after
//it outputs it
cout << "??????";
function1 is good, it executes perfectly, and i checked and release is getting fired... still unable to figure out what could be causing it to crash.
ok so i think i've gotten it fully working without crashes now, although honestly i don't know why this works. ive defined the class member functions in the header as virtual, and then static loaded the lib file instead of using LoadLibrary with the dll. any thoughts on why using the static lib file is making the difference? i understand why/when to use a static lib but im just confused on why that would make it stop crashing compared to using LoadLibrary/FreeLibrary...
Thanks for all the replies and help, much appreciated. i was so surprised to get responses so fast you guys rock!