al_destroy_sample crash
Trenchman

I'm having an issue when I try to destroy an audio sample. I create and load the sample in a class. It plays just fine, but for some reason the program crashes when the destructor tries to destroy the sample.

Here's how my code is generally setup.

#SelectExpand
1Class Player{ 2public: 3 player(){ 4 image1 = al_load_image("Assets/Image1.png"); 5 image2 = al_load_image("Assets/Image2.png"); 6 if(!image1){ 7 Error handling code 8 } 9 if(!image2){ 10 Error handling code 11 } 12 13 audio_fireball = al_load_sample("Assets/fireball.ogg"); 14 if(!audio_fireball){ 15 Error handling code 16 } 17 } 18 19 ~player(){ 20 al_destroy_bitmap(image1); 21 al_destroy_bitmap(image2); 22 std::cout << "Destroyed Images" << std::endl; //just used to test the error 23 24 al_destroy_sample(audio_fireball); 25 std::cout << "Destroyed Sample" << std::endl; //Also to test error 26 } 27 28private: 29 30 ALLEGRO_BITMAP *image1 = NULL; 31 ALLEGRO_BITMAP *image2 = NULL; 32 33 ALLEGRO_SAMPLE *audio_fireball = NULL; 34 35}

The cout after destroying the bitmaps displays, but the cout after the sample doesn't. I also did a al_is_audio_installed check right before trying to destroy the sample and it returned true.

Niunio

What do you mean with "crash"? Does it return an error message? Did you try to debug it (i.e. GDB)?

Polybios

Does your Player class happen to be instantiated as a global?

Chris Katko
Polybios said:

Does your Player class happen to be instantiated as a global?

To add to that. If it is, it'll attempt to call Allegro functions... before Allegro is initialized = boom.

Polybios

Yeah, that's true. I probably should have said "could have something to do with order of destruction".

Trenchman

I suppose I should also mention I'm using allegro 5.0.10 on a windows PC and building with Code:Blocks and MinGW.

It gives me an error window, and returns -1073741819.

I create the instance inside the main function after all of the Allegro initialization is finished. Everything seems to work fine, and destroying the images I've loaded works fine. Just when I try to destroy the sample it crashes.

Edit:
Well, I'm an idiot. For some reason I put al_uninstall_audio right before the end of main and didn't think I did. I used al_is_audio_installed to make sure it was still installed immediately before destroying the samples and it returned true. But, I think it may return true even if it isn't currently installed, as long as it was successfully installed earlier in the program. Sorry guys.

Thread #616796. Printed from Allegro.cc