dbus_g_proxy_call lets applikation crash

Eckert Martin (DC-IA/EWT1) Martin.Eckert at boschrexroth.de
Thu Nov 15 03:11:36 PST 2012


Hello,
I have the following simple C application which does both continuously sending messages to a dbus Server and receiving asynchronous messages from this server using dbus signals time by time.
Application crashes after having sent  2 -30 messages to the server with segmentation fault.
Application also works fine, when it doesn't call g_main_loop_run in the end. But then no signals can be received.
After that I used g_timeout_add(500,f_DbusSend,NULL) to send the messages and it works.
Has anybody got an idea what I'm doing wrong? Do I have to sync dbus_proxy_calls with g_main_loop? If so, has anybody an idea how to do that?

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <glib.h>
#include <dbus/dbus-glib.h>

static GMainLoop* z_o_MainLoop=NULL;
static DBusGConnection *bus = NULL;
DBusGProxy *z_o_DBusGProxy = NULL;
static pthread_t o_GMainLoopThreadId;
static pthread_t o_SendThreadId;
static GError* error;

void* f_RunGMainLoop(void* z_MainLoopAdr)
{
        g_main_loop_run(z_o_MainLoop);
        printf("f_RunMailLoop finished\n");
        return z_MainLoopAdr;
}
gboolean f_DbusSend(void* z_o_UserData)
{
        int i_PrgNr = 0, i_counterOK=1,i_counterNOK=2;
        //while(1){
                printf("Sending Counter %d, Ok: %d, Nok: %d, Proxy: %d... ", i_PrgNr,i_counterOK,i_counterNOK, *z_o_DBusGProxy);

                bool b_RetVal = dbus_g_proxy_call(z_o_DBusGProxy, "fbatchcntrset",&error, G_TYPE_INT, i_PrgNr,
                                                          G_TYPE_INT, i_counterOK++,
                                                          G_TYPE_INT, i_counterNOK,
                                                          G_TYPE_INVALID, G_TYPE_INVALID);
                if(b_RetVal){
                        printf("OK\n");
                }
                else{
                        printf("Error - %s\n", error->message);
                        g_error_free(error);
                }
                /*usleep(500000);
        }*/
}
//signal callback
void f_DbusCallbackCfgFileChanged(DBusGProxy *proxy, const char* z_c_FilePath, gpointer user_data)
{
        printf("f_DbusCallbackCfgFileChanged - %s\n", z_c_FilePath);
}
int main (void)
{
        g_type_init ();
        //new main loop
        z_o_MainLoop = g_main_loop_new(NULL, FALSE);
        bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
        if (!bus){
                printf("dbus_g_bus_get - %s\n",error->message);
                g_error_free(error);
                g_main_loop_unref(z_o_MainLoop);
                return -1;
        }
        printf("dbus_g_bus_get - OK\n");
        z_o_DBusGProxy = dbus_g_proxy_new_for_name(bus, "com.rexroth.akku.SysService", "/SysObj", "com.rexroth.akku.SysInterface");
        if( z_o_DBusGProxy==NULL){
                printf("dbus_g_proxy_new_for_name - error\n");
                g_main_loop_unref(z_o_MainLoop);
                return -1;
        }
        printf("dbus_g_proxy_new_for_name_owner - OK\n");

        //register config file changed signal
        dbus_g_proxy_add_signal(z_o_DBusGProxy, "cfgfilechanged", G_TYPE_STRING, G_TYPE_INVALID);
        dbus_g_proxy_connect_signal(z_o_DBusGProxy, "cfgfilechanged",   G_CALLBACK(&f_DbusCallbackCfgFileChanged), NULL, NULL);

        //run mainloop in separate thread
        pthread_create(&o_GMainLoopThreadId,NULL,f_RunGMainLoop,z_o_MainLoop);
        //continously send msg to server
        //g_timeout_add(500,f_DbusSend,NULL);

        //g_main_loop_run(z_o_MainLoop);
        while(1){
                f_DbusSend(NULL);
                usleep(500000);
        }

        return 0;
}

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freedesktop.org/archives/dbus/attachments/20121115/991c8f90/attachment.html>


More information about the dbus mailing list