Go Down

Topic: Memory leak in the Sprite Library (Read 705 times) previous topic - next topic

jaysee

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

robtillaart

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

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

jaysee

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.

Go Up