![]() |
|
This thread is locked; no one can reply to it.
![]() ![]() |
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:
Then all drawing is done to active page. I'm not supposed to call destroy_bitmap for active_page, right? --- |
Matthew Leverton
Supreme Loser
January 1999
![]() |
Don't destroy a bitmap if:
|
Sirocco
Member #88
April 2000
![]() |
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 --> |
CursedTyrant
Member #7,080
April 2006
![]() |
Quote: Destroying a non-existent bitmap will usually result in a crash. IIRC destroy_bitmap() doesn't crash on NULL... --------- |
Sirocco
Member #88
April 2000
![]() |
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. --> |
Felipe Maia
Member #6,190
September 2005
![]() |
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
![]() |
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
![]() |
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. |
Evert
Member #794
November 2000
![]() |
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] --- |
Sirocco
Member #88
April 2000
![]() |
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. --> |
Felipe Maia
Member #6,190
September 2005
![]() |
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
![]() |
Right, that's what I'm talking about. Guess I was misunderstood earlier in the thread. --> |
Thomas Fjellstrom
Member #476
June 2000
![]() |
Quote: Guess I was misunderstood earlier in the thread. Maybe everyone misunderstood -- |
Sirocco
Member #88
April 2000
![]() |
It's early. I'm not awake yet. Sue me. Regardless, the advice is valid. --> |
Richard Phipps
Member #1,632
November 2001
![]() |
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
![]() |
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
![]() |
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
![]() |
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:
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. __________ |
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? --- |
HoHo
Member #4,534
April 2004
![]() |
Thinking logically, yes you have to destroy them yourself because you created them. __________ |
Sirocco
Member #88
April 2000
![]() |
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. --> |
FMC
Member #4,431
March 2004
![]() |
Yes [edit]late 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] |
|
1
2
|