Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Destroying bitmaps

Credits go to Evert and Matthew Leverton for helping out!
This thread is locked; no one can reply to it. rss feed Print
 1   2 
Destroying bitmaps
Kauhiz
Member #4,798
July 2004

Is there any reason that a bitmap should not be destroyed? The reason I ask is because I get a crash when I call destroy_bitmap for some bitmaps. The bitmaps in question are loaded from a datafile, and I'm positive that they aren't being used after the destroy_* function has been called. I also have the same problem with some fonts, also loaded from a datafile. Not all the bitmaps loaded from the datafile cause a crash when destroyed, though.

Also, just for reference, in my triple buffering code I do this:

1int on_page = 0;
2 
3BITMAP *active_page;
4BITMAP *page[3];
5 
6...
7 
8page[0] = create_video_bitmap(SCREEN_W, SCREEN_H);
9page[1] = create_video_bitmap(SCREEN_W, SCREEN_H);
10page[2] = create_video_bitmap(SCREEN_W, SCREEN_H);
11
12active_page = page[0];
13 
14...
15 
16on_page = (on_page + 1) % 3;
17active_page = page[on_page];
18 
19...
20 
21destroy_bitmap(page[0]);
22destroy_bitmap(page[1]);
23destroy_bitmap(page[2]);

Then all drawing is done to active page. I'm not supposed to call destroy_bitmap for active_page, right?

---
It's Ridge Racer! RIIIIIDGE RAAAAACER!

Matthew Leverton
Supreme Loser
January 1999
avatar

Don't destroy a bitmap if:

  • It's loaded from a datafile (load_datafile, load_datafile_object, etc),

  • It still has active sub bitmaps, or

  • The mouse is displayed on it.

Sirocco
Member #88
April 2000
avatar

Quote:

Is there any reason that a bitmap should not be destroyed?

The most obvious one being... don't laugh... if you have previously destroyed it or never created it in the first place :) Destroying a non-existent bitmap will usually result in a crash. In your case you might want to make sure your create_video_bitmap calls aren't failing.

-->
Graphic file formats used to fascinate me, but now I find them rather satanic.

CursedTyrant
Member #7,080
April 2006
avatar

Quote:

Destroying a non-existent bitmap will usually result in a crash.

IIRC destroy_bitmap() doesn't crash on NULL...

---------
Signature.
----
[My Website] | [My YouTube Channel]

Sirocco
Member #88
April 2000
avatar

Quote:

IIRC destroy_bitmap() doesn't crash on NULL...

It almost always does for me, but I'm not using the very latest version of Allegro.

-->
Graphic file formats used to fascinate me, but now I find them rather satanic.

Felipe Maia
Member #6,190
September 2005
avatar

Quote:

IIRC destroy_bitmap() doesn't crash on NULL...

Most people don't NULL their stuff.

[edit]

Quote:

I'm not supposed to call destroy_bitmap for active_page, right?

No, active_page only holds the address to the bitmap, and not the bitmap itself, so you should only destroy page[0], page[1], page[2] when you exit your program.

Evert
Member #794
November 2000
avatar

Quote:

It almost always does for me, but I'm not using the very latest version of Allegro.

As far as I know, it has been valid to pass NULL to destroy_bitmap() since forever. What version of Allegro are you using?

Quote:

Is there any reason that a bitmap should not be destroyed?

What you allocate, you should free as well.

Quote:

The reason I ask is because I get a crash when I call destroy_bitmap for some bitmaps. The bitmaps in question are loaded from a datafile,

The dat entries of a datafile are freed by unload_datafile(). You did not allocate the memory for those bitmaps, so you should not destroy tham.

Quote:

I also have the same problem with some fonts, also loaded from a datafile

Same problem.

If you allocate the object yourself (ie, get it from a function that returns a BITMAP* or a FONT*), then you have to free it. Otherwise you don't. Think of it this way: you don't free Allegro's default font or the screen bitmap either.

Paladin
Member #6,645
December 2005
avatar

Simply calling

unload_datafile(datfile);

should free up all the files you used in the datafile. I may be wrong but this is always what I do. Also be sure not to free up a pointer such as active_page. It only needs to be freed if you actually load a bitmap or a create a bitmap with it. You might also call

show_mouse(NULL);

before you free all the memory that way you can be sure the mouse isn't be displayed. I could be wrong since I'm still a beginner, but I had the same problems a couple of days ago. :P

Evert
Member #794
November 2000
avatar

Quote:

You might also call

show_mouse(NULL);

before you free all the memory that way you can be sure the mouse isn't be displayed.

Only nescessary if you explicitly call set_gfx_mode() to destroy the screen (assuming that you don't display the mouse on a different surface, which is generally a silly thing to do).

Kauhiz
Member #4,798
July 2004

Yeah, that makes sense, thanks guys.

[EDIT]
One more question: if I create sub-bitmaps of a bitmap I loaded from a datafile, do I have to destroy the sub-bitmaps?

---
It's Ridge Racer! RIIIIIDGE RAAAAACER!

Sirocco
Member #88
April 2000
avatar

Quote:

Most people don't NULL their stuff.

Quote:

As far as I know, it has been valid to pass NULL to destroy_bitmap() since forever. What version of Allegro are you using?

Sure, but this code seg faults every time for me:

pseudo code crap below:

BITMAP *foo;

void some_routine()
{
.
// We never created the bitmap
.
destroy_bitmap(foo);
}

Right now I think I'm using 4.03.

-->
Graphic file formats used to fascinate me, but now I find them rather satanic.

Felipe Maia
Member #6,190
September 2005
avatar

You're just declaring foo, so it has the value that was previously on the memory, which can be anything. Do this not to segfault:

BITMAP *foo = NULL;

void some_routine()
{
.
.
.
destroy_bitmap(foo);
}

Sirocco
Member #88
April 2000
avatar

Right, that's what I'm talking about. Guess I was misunderstood earlier in the thread.

-->
Graphic file formats used to fascinate me, but now I find them rather satanic.

Thomas Fjellstrom
Member #476
June 2000
avatar

Quote:

Guess I was misunderstood earlier in the thread.

Maybe everyone misunderstood ;) You're talking about uninitialized variables being "free"d, while everyone else is talking specifically about passing NULL (or variables set to NULL) to destroy_bitmap. These are two very different things.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Sirocco
Member #88
April 2000
avatar

It's early. I'm not awake yet. Sue me. Regardless, the advice is valid.

-->
Graphic file formats used to fascinate me, but now I find them rather satanic.

Richard Phipps
Member #1,632
November 2001
avatar

Aren't global uninitialised variables (including Bitmaps) set to NULL? I thought only local variables could hold any value.

axilmar
Member #1,204
April 2001

What about bitmap destruction on program exit? I have my bitmaps encapsulated in a Bitmap class (C++) which is destroyed probably after allegro exits. Does allegro destroy its bitmaps automatically on exit?

Richard Phipps
Member #1,632
November 2001
avatar

Quote:

Does allegro destroy its bitmaps automatically on exit?

No.

axilmar
Member #1,204
April 2001

Then why I can not destroy a bitmap after allegro exits? the program crashes within the destructor of the Bitmap class calling the 'destroy_bitmap' function.

I guess it may be something with allegro cleanup taking place before bitmap destruction. Is there a way to destroy the bitmaps before allegro exits?

Richard Phipps
Member #1,632
November 2001
avatar

Can't you call the cleanup function before your program ends? Or can you not add with atexit a cleanup function?

HoHo
Member #4,534
April 2004
avatar

Quote:

Aren't global uninitialised variables (including Bitmaps) set to NULL?

it is not quaranteed. I think GCC does that only in debug mode

Quote:

I thought only local variables could hold any value.

huh?

As for cleaning up on program exit, I would simply do something like this:

1Object *bunc, *of, *globals;
2 
3void init(){
4 intAllegro();
5 initGlobalObjects();
6}
7void run(){
8 while (gameGoesOn){
9 doStuff();
10 }
11}
12void exit(){
13 freeGlobalObjects();
14}
15 
16int main(){
17 init();
18 run();
19 exit();
20}
21END_OF_MAIN()

I would stay away from non-pointer global objects that have destructors that free allegro objects. The order of destroying objects is not defined and when allegro itself closes before your allegro objects are freed you are screwed. With global pointers you should have no such problems.

__________
In theory, there is no difference between theory and practice. But, in practice, there is - Jan L.A. van de Snepscheut
MMORPG's...Many Men Online Role Playing Girls - Radagar
"Is Java REALLY slower? Does STL really bloat your exes? Find out with your friendly host, HoHo, and his benchmarking machine!" - Jakub Wasilewski

Kauhiz
Member #4,798
July 2004

me said:

One more question: if I create sub-bitmaps of a bitmap I loaded from a datafile, do I have to destroy the sub-bitmaps?

Well?

---
It's Ridge Racer! RIIIIIDGE RAAAAACER!

HoHo
Member #4,534
April 2004
avatar

Thinking logically, yes you have to destroy them yourself because you created them.

__________
In theory, there is no difference between theory and practice. But, in practice, there is - Jan L.A. van de Snepscheut
MMORPG's...Many Men Online Role Playing Girls - Radagar
"Is Java REALLY slower? Does STL really bloat your exes? Find out with your friendly host, HoHo, and his benchmarking machine!" - Jakub Wasilewski

Sirocco
Member #88
April 2000
avatar

Quote:

One more question: if I create sub-bitmaps of a bitmap I loaded from a datafile, do I have to destroy the sub-bitmaps?

The general rule is that anything you create, you must destroy.

-->
Graphic file formats used to fascinate me, but now I find them rather satanic.

FMC
Member #4,431
March 2004
avatar

Yes

[edit]late :P
but i add this:

the manual said:

Return value: Returns a pointer to the created sub bitmap, or NULL if the sub bitmap could not be created. Remember to free the sub bitmap before freeing the parent bitmap to avoid memory leaks and potential crashes accessing memory which has been freed.

[FMC Studios] - [Caries Field] - [Ctris] - [Pman] - [Chess for allegroites]
Written laws are like spiders' webs, and will, like them, only entangle and hold the poor and weak, while the rich and powerful will easily break through them. -Anacharsis
Twenty years from now you will be more disappointed by the things that you didn't do than by the ones you did do. So throw off the bowlines. Sail away from the safe harbor. Catch the trade winds in your sails. Explore. Dream. Discover. -Mark Twain

 1   2 


Go to: