Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Spot the Memory Leak (right forum this time)

Credits go to ReyBrujo for helping out!
This thread is locked; no one can reply to it. rss feed Print
Spot the Memory Leak (right forum this time)
kcowolf
Member #8,682
May 2007

I'm using Wu Yongwei's debug_new.cpp to try to root out memory leaks in my current project, and I seem to have them all except for one.

Here's where the code is allocated:

Players[iCurrentPlayer].sprAnim = new BITMAP**[Players[iCurrentPlayer].iAnimSteps];
for (int i = 0; i < Players[iCurrentPlayer].iAnimSteps; i++)
{
  Players[iCurrentPlayer].sprAnim<i> = new BITMAP*[6];
  for (int j = 0; j < 6; j++)
  {
    fprintf(stderr, "Creating sprAnim[%i][%i]\n", i, j);
    Players[iCurrentPlayer].sprAnim<i>[j] = new BITMAP; // THIS LINE CAUSES LEAKS
  }
}

And so on. The line with the comment is the one always indicated by debug_new. I have a struct called objPlayers (Players is a pointer to an array of objPlayers). Here's the destructor:

objPlayer::~objPlayer()
{
  for (int i = 0; i < iAnimSteps; i++)
  {
    for (int j = 0; j < 6; j++)
    {
      fprintf(stderr, "Destroying sprAnim[%i][%i]\n", i, j);
      destroy_bitmap(sprAnim<i>[j]);    
    }
    delete[] sprAnim<i>;
  }
  delete[] sprAnim;
}

I'm using Allegro 4.2.1 in Windows XP with the MinGW compiler (and MSYS environment). I'm testing with three frames of animation (frame 0, 1, and 2). sprAnim should always be sprAnim[frame number][object number (0 through 5)]. Does anyone have some insight they could give me?

ReyBrujo
Moderator
January 2001
avatar

You create bitmaps with create_bitmap and not with new. The destroy_bitmap call is not freeing the memory because the structure was not created with create_bitmap.

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

Goalie Ca
Member #2,579
July 2002
avatar

It probably also has to do with the fact that Bitmap is a struct containing a pointer to the bitmap and not the bitmap itself. So new shouldn't actually allocate any memory.

-------------
Bah weep granah weep nini bong!

kcowolf
Member #8,682
May 2007

Reybujo said:

You create bitmaps with

create_bitmap

and not with new. The destroy_bitmap call is not freeing the memory because the structure was not created with create_bitmap.

D'oh! That was the problem exactly. I just took out that for (int = j) loop and it worked perfectly. Thanks for the help!

Go to: