Pages: [1]   Go Down
Author Topic: Memory leak in the Sprite Library  (Read 677 times)
0 Members and 1 Guest are viewing this topic.
Switzerland
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Let's play with Arduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

I just found out that there is a bug in the sprite library, as the memory allocated for the storage of a sprite (i.e. _buffer) never gets deallocated. The destructor of a sprite is missing.

I had to add the following code to Sprite.cpp:

===================================================

#include <string.h>

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;
}

===================================================

And consequently the following lines in Sprite.h:

===================================================

   Sprite(const Sprite &other);
   ~Sprite();
   Sprite& operator=(const Sprite &other);

===================================================

I hope this helps other people playing around with the Sprite library and run into strange hangings of the program caused by the lack of free SRAM.

Perhaps this addition finds its way into a further release of the Arduino software.

Kind regards,

Jean-Claude
Logged

Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 220
Posts: 13846
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

DO you have informed the makers of the sprite lib too ? (assuming their names are somewhere near where you downloaded it )?
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Switzerland
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Let's play with Arduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The Sprite library is part of the Standard Libraries and is included in the Arduino framework: http://arduino.cc/en/Reference/Libraries
I did not find out who is the maintainer of the library, will ask the guys from the Wiring project.
Logged

Pages: [1]   Go Up
Jump to: