Search code examples
cglibgdbus

gdbus: is it safe to free signal argument right after signal emission


I created simple dbus service that emits signal with dynamically allocated data argument:

file_name = g_strdup("myfile");
...
...
g_signal_emit_by_name (object, "mysignal", file_name);
g_free(file_name);

In this case signal listeners may receive file_name string that was already destroyed.

So is it safe to free file_name right after g_signal_emit_by_name call or I should wait for a few seconds? Or is there any other mechanism to free memory in such cases?


Solution

  • GSignal emission is synchronous, i.e. all the connected callbacks to a signal are run sequentially by g_signal_emit(), which will return control to you once all callbacks return. thus, it is safe to emit a signal and free the arguments of the signal after g_signal_emit() returns.

    if you're using DBus then it's still safe: the data will be copied over to the receiving process(es), as it would be impossible to share it across process boundaries.