Hey guys. Recently I added a line of code in my class destructor that deletes a window object that was declared as a pointer. It looked like this.
1 2 3
Class::~Class() {
delete window;
}
This didn't work. All it did was cause an assertion error. Can anyone explain to me why this doesn't work?
Note: The window object is redeclared inside of most classes and the original object is passed via pointers through the constructor of most object. The one I'm deleting isn't the original object.
Then you are deleting memory that the class "logically" doesn't own.
If your class does this:
window = new Window(); //Then you need to delete it
However, if it is done like this, which is the way you describe it.
1 2 3 4
Class(Window * pWindow) : window(pWindow)
{}
//Then no, you do not want to delete it...
The use of pointers does not mean that you have to delete/free memory. You only delete/free memory that you allocate, declaring a pointer is no different than any other variable.