Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Memory problems with this loop

This thread is locked; no one can reply to it. rss feed Print
Memory problems with this loop
alex glez
Member #16,757
October 2017

Hello again. I have a problem with the creation, loading and destruction of bitmaps.
The next loop consumes all the system memory until an error occurs .. Can not be done without this happening?

ALLEGRO_BITMAP *PRUEBA = NULL;
------------

for (w2 = 0; w2 < 1000; w2++)
{
PRUEBA = al_create_bitmap(500, 333);
PRUEBA = NULL;
PRUEBA = al_load_bitmap("d:\\500.png"); // 500 x 333
al_destroy_bitmap(PRUEBA);
}
-------------

This is just a test, but it is very important to adapt it to my program. Thank you

Mark Oates
Member #1,146
March 2001
avatar

OK, a couple things happening here. When you call al_create_bitmap you are creating a bitmap in memory. The address of that new bitmap is then assigned to PRUEBA. After that you are re-assigning to PRUEBA the value of NULL, which means that the bitmap you created with al_create_bitmap is still dangling in memory and has not been freed.

After that, you are loading another bitmap with al_load_bitmap("d:\\500.png"); which 1) allocates space for a bitmap in memory and 2) initializes the values in that data to the contents in the 500.png file.

Then you are freeing the bitmap from memory that was allocated when you called al_load_bitmap by calling al_destroy_bitmap(PRUEBA);

(also, you can wrap your code in <code> tags </code> and it will look like below:)

ALLEGRO_BITMAP *PRUEBA = NULL;
------------

for (w2 = 0; w2 < 1000; w2++)
{
PRUEBA = al_create_bitmap(500, 333);
PRUEBA = NULL;
PRUEBA = al_load_bitmap("d:\\500.png"); // 500 x 333
al_destroy_bitmap(PRUEBA);
}

--
Visit CLUBCATT.com for cat shirts, cat mugs, puzzles, art and more <-- coupon code ALLEGRO4LIFE at checkout and get $3 off any order of 3 or more items!

AllegroFlareAllegroFlare DocsAllegroFlare GitHub

alex glez
Member #16,757
October 2017

ok thanks, I understand, here there are plenty of al_create_bipmap and PROOF = NULL.
Your advice helped me a lot, I appreciate it.

My mind still thinks of gwbasic and msx-basic .. Regards

Go to: