From b0821c50dc30649c571982b210e29a97311e3045 Mon Sep 17 00:00:00 2001 From: Frederic Suter Date: Sun, 9 Feb 2020 15:43:59 +0100 Subject: [PATCH] fix refcount issue If you create an actor and know that you may try to do something on it after its completion, you'd better take an extra reference on it. Once you're done with this actor, do not forget to release the reference or you will face some memory leaks. --- examples/c/actor-kill/actor-kill.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/examples/c/actor-kill/actor-kill.c b/examples/c/actor-kill/actor-kill.c index 2d450bbbc6..d0795cd801 100644 --- a/examples/c/actor-kill/actor-kill.c +++ b/examples/c/actor-kill/actor-kill.c @@ -39,8 +39,11 @@ static void killer_fun(XBT_ATTRIB_UNUSED int argc, XBT_ATTRIB_UNUSED char* argv[ XBT_INFO("Hello!"); /* - First start a victim process */ sg_actor_t victimA = sg_actor_init("victim A", sg_host_by_name("Fafard")); sg_actor_start(victimA, victimA_fun, 0, NULL); + sg_actor_t victimB = sg_actor_init("victim B", sg_host_by_name("Jupiter")); + sg_actor_ref(victimB); // We have to take that ref because victimB will end before we try to kill it sg_actor_start(victimB, victimB_fun, 0, NULL); + sg_actor_sleep_for(10.0); XBT_INFO("Resume the victim A"); /* - Resume it from its suspended state */ @@ -53,6 +56,7 @@ static void killer_fun(XBT_ATTRIB_UNUSED int argc, XBT_ATTRIB_UNUSED char* argv[ XBT_INFO("Kill victimB, even if it's already dead"); /* that's a no-op, there is no zombies in SimGrid */ sg_actor_kill(victimB); // the actor is automatically garbage-collected after this last reference + sg_actor_unref(victimB); // Release the ref taken on victimB to avoid to leak memory sg_actor_sleep_for(1.0); XBT_INFO("Start a new actor, and kill it right away"); -- 2.20.1