Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Unloading datafile causes program to crash.

This thread is locked; no one can reply to it. rss feed Print
Unloading datafile causes program to crash.
XcronZ3X
Member #8,222
January 2007

Ok so here's basically the code:

allegro_message("Sounds destroyed!"); //debug
unload_datafile(data);
allegro_message("Datafile unloaded!"); //debug
remove_sound();
allegro_message("Datafile unloaded and sound removed!"); //debug
allegro_exit();
}
END_OF_MAIN();

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??

Matthew Leverton
Supreme Loser
January 1999
avatar

You're freeing the stuff twice. unload_datafile will unload all the objects. Don't manually unload the individual elements beforehand.

XcronZ3X
Member #8,222
January 2007

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..

Matthew Leverton
Supreme Loser
January 1999
avatar

This is broken:

DATA *dat = load_datafile("foo.dat");
BITMAP *bmp = dat[foo].dat;
destroy_bitmap(bmp);
unload_datafile(dat);

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.

Jonatan Hedborg
Member #4,886
July 2004
avatar

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.

-------
Sweden: Free from the shackles of Democracy since 2008-06-18!

Tobias Dammers
Member #2,604
August 2002
avatar

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.

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

Go to: