0

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!

misteryes
  • 2,167
  • 4
  • 32
  • 58

1 Answers1

0

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().

Remi Gacogne
  • 4,655
  • 1
  • 18
  • 22