Pregunta

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?

¿Fue útil?

Solución

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.

Otros consejos

I'm not familiar with the gdbus API in particular, but in general for this kind of usage, you want to make it the recipient's responsibility to free the storage. The only exceptions would be when the object is known to have a lifetime that will persist beyond the needs of the recipient (e.g. static storage) or the API handles copying data for you.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top