Hi,<br><br>studying the low level API Tutorial at <a href="http://dbus.freedesktop.org/doc/api/html/index.html">http://dbus.freedesktop.org/doc/api/html/index.html</a><br>I want to make sure that I've understood the concept of getting arguments of a received message correctly.<br>
<br>If I want to keep the argument of a received message I have to allocate new memory and copy the value of the argument (which is presented by a pointer).<br>After that I have to unref the message. <br>Here an example of a possible function (pseudo-code):<br>
<br>char * myReceiveMethod(void){<br> DBusMessage* msg;<br> DBusMessage* reply;<br> char *resultarg;<br> char *buf=NULL;<br><br> (....)<br><br> if (!dbus_message_get_args (reply, &err, <br> DBUS_TYPE_STRING, &resultarg,<br>
DBUS_TYPE_INVALID)){ <br> fprintf (stderr, "Failed to complete ListServices call: %s\n",<br> err.message);<br> exit (1);<br> } <br><br> //copy value of received argument and make necessary memory allocation<br>
buf = (char *)malloc(sizeof(char) * (strlen(resultarg) +1));<br> strcpy(buf, resultarg); <br> //unref messages, <br clear="all"> dbus_message_unref(reply);<br> dbus_message_unref(msg);<br> <br> return buf; //return pointer, to do anything with it, of course it still must be "free'ed" when Im done with it ( free() )<br>
}<br><br>The adress where resultarg points to is not valid anymore after the reply was unrefed, right?<br><br><br><br>Handling reply arguments of type array is different.<br>Here you don't have to allocate new memory If you want to keep the arguments.<br>
But you have to free the memory after the message reply was unrefed, although you haven't allocated the memory.<br>Example:<br><br>char ** myMethod(){<br> DBusMessage* msg;<br> DBusMessage* reply;<br> char **services;<br>
int servicesCount=0<br><br>(...)<br><br> if (!dbus_message_get_args (reply, &err, <br> DBUS_TYPE_ARRAY,DBUS_TYPE_STRING,<br> &services,&servicesCount, <br> DBUS_TYPE_INVALID)){ <br>
fprintf (stderr, "Failed to complete GetRemoteServiceClasses call: %s\n",<br> err.message);<br><br> dbus_message_unref(reply);<br> dbus_message_unref(msg);<br><br><br> // for (i = 0; i < servicesCount; i++)<br>
// free (services[i]);<br> // free (services);<br><br>//<br><br> return services;<br> } <br><br><br>Of course I still have to free the services when Im done with them, e.g. before exiting the application, for example wit the following code:<br>
for (i = 0; i < servicesCount; i++)<br>
free (services[i]);<br>
free (services);<br><br><br>Thanks for your comments and hints.<br><br>-- <br>Regards,<br>--Codefritz<br><br><br><br>