Unloading datafile causes program to crash.
Ok so here's basically the code:
allegro_message("Sounds destroyed!"); //debug
allegro_message("Datafile unloaded!"); //debug
allegro_message("Datafile unloaded and sound removed!"); //debug
The sounds destroyed message comes up and then BOOM! The program crashes! This is pretty strange and I cannot seem to think of what the reason may be. Before the destruction of the sounds, I also free up and destroy bitmaps. All the bitmaps and sounds, etc., come from the datafile and I was thinking that that may possibly be the problem but I see no reason for that to be so...does the call to unload_datafile() do something I'm not aware of other than freeing up all the objects in the datafile and removing it from memory??
You're freeing the stuff twice. unload_datafile will unload all the objects. Don't manually unload the individual elements beforehand.
Mmmm, so it was as I thought...but it feels strange that unload_datafile would even take care of all of the memory that I used when taking objects from the datafile and putting them into objects that I create while the program is running..
It also feels weird that by freeing the objects I create while the program is running go all the way to the root objects from the datafile and free them up as well..
This is broken:
But if you are blitting the object to another bitmap you create, then you must unload both. It's simple to remember. If you use create/load_bitmap, then you need to destroy it.
Remember, XcronZ3X - A pointer (BITMAP *bmp) just "points" to a chunk of data, in this case a bitmap. When you do BITMAP *bmp = dat[foo].dat; you copy the pointer to the bitmap data - not the actual bitmap data.
Thread #589573. Printed from Allegro.cc
In other words, treat each datafile in only one of the following ways.
a) Use load_datafile() to load all of its content into memory, use it, finally use unload_datafile() to free the loaded content. Do not free() or destroy_XXXX() anything from the datafile manually.
b) Load objects one by one using load_datafile_object(); unload using unload_datafile_object(). The same caveats as for a) hold true.
c) Treat the datafile as a directory, and load objects using the #-syntax provided by the normal loading functions (e.g. load_bitmap("my_datafile#sprite.bmp"); ). Objects loaded this way can and should be freed using the appropriate destroy_XXXX() routines.
Whatever you do, don't mix the above unless you know exactly what you're doing.