-
Notifications
You must be signed in to change notification settings - Fork 168
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Destructor in Sprite Library missing #17
Comments
Already reported this bug on google code but it did not get fixed yet. |
Have you verified that the destructor gets called? I seem to remember that the current implementation of delete does not call destructors? Let me know and I'll fix this problem if the destructors actually gets called. (Either way I think I'll fix this, just because it's good practice.) |
Dear Alexander, I verified that the destructor gets called! Delete always calls the destructor. Can be a problem when using derived classes and pointers to this derived classes in which case you have to define a virtual destructor as explained in: http://www.programmerinterview.com/index.php/c-cplusplus/virtual-destructors/ |
With the new tool chain I think we're ready to support virtual destructors, as a note to self: Remember to define |
The destructor is missing in the sprite library (and also in the matrix library) which leads to a memory leak when dynamically creating sprites. When adding a destructor it becomes necessary to add a copy and an assignment operator.
The following addtions should be made to the sprite library:
Sprite::Sprite(const Sprite &other)
{
operator=(other); // Call the assignment operator
}
Sprite::~Sprite()
{
if (!_buffer) return;
free(_buffer); // Make sure to free the buffer if it exists
}
Sprite& Sprite::operator=(const Sprite &other)
{
_width = other._width;
_height = other._height;
init(_width, _height);
memcpy(_buffer,other._buffer,_height);
return *this;
}
Kind regards,
Jean-Claude
The text was updated successfully, but these errors were encountered: