简体   繁体   中英

can libevent `struct event` variable be modified?

in on callback function on_accept , I make a event conn_ev

    conn_ev = (struct event *)malloc(sizeof(struct event));
    event_set(conn_ev, connfd, EV_READ, on_recv, conn_ev);
    event_base_set(base, conn_ev);
    event_add(conn_ev, NULL);

the callback function on_recv will be triggered when there is a new connection.

and in the callback function on_recv(int connfd, short event, void *conn_event) , I have

    conn_ev = (struct event *) conn_event;
    event_set(conn_ev, connfd, EV_WRITE, on_send, conn_ev);
    event_base_set(base, conn_ev);
    event_add(conn_ev, NULL);

so the conn_ev is modified in this callback function.

are there any possible problem/trap here so that it is better I malloc a new conn_ev?

thanks!

You have to make sure that the event is not used before modifying it. If you are in the callback of this particular event and it's not a persistent one, you may be fine, but I would advise to call event_del() in any case.

While it may work, you should not use malloc() and event_set (which is now deprecated). Use event_new() instead, or at least replace event_set with event_assign().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM