Say I have the following in a DLL implementation (eg, it would have a cpp file):
class Base
{
protected:
Something *some;
public:
virtual void init()
{
some = new Something();
}
virtual ~Base()
{
delete some;
}
};
Then in my exe I make:
class Derived : public Base
{
public:
virtual void init()
{
some = new SomethingElse();
}
};
int main()
{
Base *blah = new Derived;
delete blah;
}
Would this ever cause problems if the DLL is ran with a different runtime than the exe?
if so, is there a non boost, non c++ 0x solution
Thanks
I think you need to write
~Derive()like thisExplanation :
Why you need to write this even though the
~Base()does the same thing (it looks it does the same thing) is because~Derived()ensures that you delete your object from the same heap/memory-pool/etc they were created on.See these topics:
How to use a class in DLL?
Memory Management with returning char* function
EDIT:
Better would be to add one more virtual function, say
deinit(), (a counter-part of yourvirtual void init()) , redefine this too when you redefineinit(), and do the de-allocation there indeinit().