![]() |
|
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. 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
![]() |
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
![]() |
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. -- |
knudel
Member #13,235
August 2011
|
ahh, ok |
|