Debug assertion error when deleting class
I have a dll that contains a global variable. This global variable is a class:
In an exported function I call:
Code:
mcClass = new MyClass(...);
which obviously constructs the class. However, I then wanted to add the ability to call that function, but if the var mcClass has already been initialised, I wanted it to delete it first. So, in DllMain I placed:
On the call to my function it would therefore do:
Code:
if(mcClass != NULL)
delete mcClass;
mcClass = new MyClass;
However, when this executes in Debug mode, it causes an Assertion Error:
Code:
Expression: _CrtIsValidHeapPointer(pUserData)
Why is this the case? Surely if the class is global, it can be deleted wherever?
Thanks
HD