WiringProject / Wiring

Wiring Framework
http://wiring.org.co/
Other
217 stars 168 forks source link

Destructor in Sprite Library missing #17

Open jayseeee opened 12 years ago

jayseeee commented 12 years ago

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

jayseeee commented 12 years ago

Already reported this bug on google code but it did not get fixed yet.

AlexanderBrevig commented 11 years ago

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.) PS: Maybe I'm confusing this with virtual destructors?

jayseeee commented 11 years ago

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/

AlexanderBrevig commented 10 years ago

With the new tool chain I think we're ready to support virtual destructors, as a note to self: Remember to define int cxa_guard_acquire(guard g) {return !(char _)(g);}; void cxa_guard_release (guard g) {_(char )g = 1;}; void cxa_guard_abort (guard *) {}; void __cxa_pure_virtual(void) {};