I ran into a heap corruption today caused by different CRT settings (MTd MDd) in my dll and my actual project. What I found strange is that the application only crashed when I set the destructor in the dll to be virtual. Is there an easy explanation for that? I get that I can't free memory that's not on my heap, but where exactly is the difference when I define the destructor as non-virtual.
Some Code just to make it a little clearer
The DLL
#pragma once
class CTestClass
{
public:
_declspec(dllexport) CTestClass() {};
_declspec(dllexport) virtual ~CTestClass() {};
};
And my project
int main(int argc, char* argv[])
{
CTestClass *foo = new CTestClass;
delete foo; // Crashes if the destructor is virtual but works if it's not
}
class _declspec(dllexport) CTestClass {...}
) and remove the per-member declspecs ? Just curious. And note, the calling code and the DLL should be using the same CRT (debug or release), so thats something to consider. I'm not even sure mixed-modes is supported (I don't think it is). – Homosporous