Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » user event ref counting problem

This thread is locked; no one can reply to it. rss feed Print
user event ref counting problem
Sancha
Member #14,714
November 2012

Why does my_event_dtor function get called for each call to al_unref_user_event?

Surely the event queue should contain 3 events prior to entering the msg pump loop?
I would expect to see the dtor function called only once after the ref count reached 0

#SelectExpand
1#include <allegro5/allegro.h> 2#include <allegro5/allegro_native_dialog.h> 3 4#define MY_COMPLEX_EVENT_TYPE ALLEGRO_GET_EVENT_TYPE('m', 'c', 'e', 't') 5 6ALLEGRO_EVENT_QUEUE *m_queue; 7ALLEGRO_EVENT_SOURCE m_userSrc; 8ALLEGRO_EVENT m_userEvent; 9ALLEGRO_EVENT event; 10 11int cnt = 0; 12 13static void my_event_dtor(ALLEGRO_USER_EVENT *ev) 14{ 15al_show_native_message_box(NULL, NULL, "Event", "MY_COMPLEX_EVENT_TYPE my_event_dtor called", NULL, ALLEGRO_MESSAGEBOX_ERROR); 16} 17 18int main(int argc, char **argv) 19{ 20 al_init(); 21 al_init_user_event_source(&m_userSrc); 22 m_queue = al_create_event_queue(); 23 al_register_event_source(m_queue, &m_userSrc); 24 25 m_userEvent.user.type = MY_COMPLEX_EVENT_TYPE; 26 al_emit_user_event(&m_userSrc, &m_userEvent, my_event_dtor); 27 al_emit_user_event(&m_userSrc, &m_userEvent, my_event_dtor); 28 al_emit_user_event(&m_userSrc, &m_userEvent, my_event_dtor); 29 30 while (true) 31 { 32 al_wait_for_event(m_queue, &event); 33 if (event.type == MY_COMPLEX_EVENT_TYPE) 34 { 35 al_show_native_message_box(NULL, NULL, "Event", "MY_COMPLEX_EVENT_TYPE", NULL, ALLEGRO_MESSAGEBOX_ERROR); 36 al_unref_user_event(&event.user); 37 } 38 } 39 return 0; 40}

Elias
Member #358
May 2000

No, al_emit_user_event creates a new event. The reference counting ensures that for each call to al_emit_user_event you get one call of the destructor (once al_unref_user_event has been called on it from all the queues it was sent to).

--
"Either help out or stop whining" - Evert

Sancha
Member #14,714
November 2012

Ok, that makes sense. Thanks

Go to: