On Fri, Aug 23, 2013 at 6:23 AM, Timir Karia <[email protected]> wrote: > Hi All, > > I'm using libevent 2.1.3-alpha-dev and was wondering how I could pass an > event to a callback function as part of another structure. In code I would > like the following: > > struct my_struct { > struct event* evt; > struct foo* f; > int x > }; > > void event_callback(evutil_socket_t sock, short what, void *user_data) { > struct my_struct* data = (struct my_struct*)user_data; > another_function(what, data->evt); > } > > Trouble is when I create and add the event the only option to pass the event > itself is using event_self_cbarg() as follows: > > struct event* new_event; > new_event = event_new(base, fd, EV_WRITE, event_callback, > event_self_cbarg()); > > My question is would it be safe to create my_struct and pass it to event_new > and then fill in the evt member of my_struct later on by using > my_struct->evt = event_self_cbarg()?
No, but it's fine to do this: struct my_struct *arg; struct event* new_event; arg = malloc(sizeof struct my_struct); new_event = event_new(base, fd, EV_WRITE, event_callback, arg); arg->evt = new_event; The event_self_cbarg() trick is only for the case where you need to make an event be its own user_data. peace, -- Nick *********************************************************************** To unsubscribe, send an e-mail to [email protected] with unsubscribe libevent-users in the body.
