Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » A5: destroy_timer() doesn't work?

This thread is locked; no one can reply to it. rss feed Print
A5: destroy_timer() doesn't work?
knudel
Member #13,235
August 2011

Hi,

Maybe this question is a bit dumb but in this example the timer works after al_destroy_timer() completely correct. I thougt it should return an error or something else but it is no problem to use a timer after it is destroyed.

#SelectExpand
1#include <allegro5/allegro.h> 2#include <iostream> 3 4using namespace std; 5 6int main(){ 7 al_init(); 8 ALLEGRO_TIMER *timer = al_create_timer(1); //create the timer 9 al_destroy_timer(timer); //and destroy it 10 11 ALLEGRO_EVENT_QUEUE *qu = al_create_event_queue(); 12 al_register_event_source(qu,al_get_timer_event_source(timer)); 13 14 al_start_timer(timer); 15 while(true){ 16 ALLEGRO_EVENT ev; 17 al_wait_for_event(qu, &ev); 18 cout << al_get_timer_count(timer) << endl; //the timer works correct 19 } 20}

Is this a bug or am i an idiot? ???

thanks for all answers

Matthew Leverton
Supreme Loser
January 1999
avatar

It's a bug in your code, yes. See the Allegro source:

/* Function: al_destroy_timer
 */
void al_destroy_timer(ALLEGRO_TIMER *timer)
{
   if (timer) {
      al_stop_timer(timer);

      _al_unregister_destructor(_al_dtor_list, timer);

      _al_event_source_free(&timer->es);
      al_free(timer);
   }
}

You are just using memory that you no longer own. It may "work," but it could explode at any moment.

Thomas Fjellstrom
Member #476
June 2000
avatar

You'll have problems later when something else starts using the memory that timer previously used. You should never use objects after they have been freed.

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

knudel
Member #13,235
August 2011

ahh, ok :)
thank you very much for the answers

Go to: