A5: destroy_timer() doesn't work?


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.

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

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_unregister_destructor(_al_dtor_list, timer);


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

Thomas Fjellstrom

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.


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

Thread #608074. Printed from Allegro.cc