I have been working on a code in which I have a lot of classes. I am allocating memory to different arrays of objects in constructor. However I had a strange error while I thought everything was ok. for an example lets say I have a class named Points and it has a double array of points called data.
Okay I am posting all of the code now:
class Points
{
double *data;
Points::Points()
{
data = new double [C_NUMBER_OF_POINTS];
}
Points::~Points()
{
delete [] this->data;
}
};
After debugging I found out that the error was with the this pointer, which I do not know why it is? The destructor is called to delete data, while the object is being destroyed, but it is still in memory. My question is why is it like this?
The error I was getting is basically due to mishandling of memory
Unhandled exception at 0x778f15de in HandTracker.exe: 0x00000000: The operation completed successfully. Blockquote
The error is fixed if I remove this pointer meaning if I use the following destructor
Points::~Points()
{
delete []data;
}
My question is not exactly about how to handle memory leaks, it's about this specific problem related with this pointer. What is the mechanism behind this pointer which makes it give this error?