GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Mon Sep 15, 2014 9:30 am

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Issue with gtk_widget_show
PostPosted: Sun Feb 09, 2014 12:52 pm 
Offline

Joined: Sat Feb 08, 2014 9:59 am
Posts: 3
Hi,

I'm developping a c++ client/server application and I'm making a Gui with Gtk+ for the server side.
I have an issue with Gtk+, in fact each time a client connects to the server I want him to appears in my Gui.
When a first client is connected, everything works but when the second coming the app crashes.
And I get this error from my terminal
Quote:
**
Gtk:ERROR:/build/buildd/gtk+3.0-3.4.2/./gtk/gtkcontainer.c:3292:gtk_container_propagate_draw: assertion failed: (gtk_widget_get_parent (child) == GTK_WIDGET (container))
Abandon (core dumped)


I have a function which is called when a client is just connected, and when I comment it the app works just fine.
I kept it simple for the beginning:

Code:
void ClientsController::addClient(const char *hostName){
        GtkWidget *clientBox;                               
        GtkWidget *clientLabel;
        char msg[256];

        ConsoleController *console = ConsoleController::getInstance();
                                                                       
        clientBox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 1);
        clientLabel = gtk_label_new(hostName);                   
        gtk_box_pack_end(GTK_BOX(clientBox), clientLabel, TRUE, TRUE, 0);
        gtk_container_add(GTK_CONTAINER(clientsBox), GTK_WIDGET(clientBox));
        gtk_widget_show(clientBox);
        gtk_widget_show(clientLabel);         


        snprintf(msg, 255, "New client: %s connected\n", hostName);
        console->write(msg);
}


clientsBox is another box container which is got by a gtk_builder.
In fact when I comment the two gtk_widget_show calls the app does not crash.
Any idea why?

thanks!


Top
 Profile  
 
 Post subject: Re: Issue with gtk_widget_show
PostPosted: Sun Feb 09, 2014 5:07 pm 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 745
Location: UK
Hello,

There are many things that are unknown here. There is no indication of what type of widget "clientsBox" is. Certain types of of containers can only have one widget.

Also are you using multiple threads? If you call GTK from different threads you will have problems.

_________________
E.


Top
 Profile  
 
 Post subject: Re: Issue with gtk_widget_show
PostPosted: Sun Feb 09, 2014 7:46 pm 
Offline

Joined: Sat Feb 08, 2014 9:59 am
Posts: 3
Yes, I forgot to mention that ClientsBox is a GtkBox.
But indeed, I call gtk functions from a thread which handles the new coming clients.
As I am a Gtk beginner, I was not aware that this could be unstable, I'll search how I can solve this issue.
I'll keep you informed, thanks for your answer!


Top
 Profile  
 
 Post subject: Re: Issue with gtk_widget_show
PostPosted: Mon Feb 10, 2014 7:52 am 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 745
Location: UK
This is one solution that I would use myself. The GLib function g_idle_add() puts the function addClient_main_thread() in a queue to be called when the GTK/Glib main loop has nothing else to do. This has the effect of running this function in the main thread and not a child thread keeping all your GTK function calls in the same thread.
Code:
static gboolean addClient_main_thread(gpointer userdata)
{
    const char *hostName = (const char *) userdata;
    char msg[256];

    ConsoleController *console = ConsoleController::getInstance();
                                                                       
    GtkWidget *clientBox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 1);
    GtkWidget *clientLabel = gtk_label_new(hostName);

    gtk_box_pack_end(GTK_BOX(clientBox), clientLabel, TRUE, TRUE, 0);
    gtk_container_add(GTK_CONTAINER(clientsBox), GTK_WIDGET(clientBox));
    gtk_widget_show(clientBox);
    gtk_widget_show(clientLabel);

    snprintf(msg, 255, "New client: %s connected\n", hostName);
    console->write(msg);
    g_free(hostName);

    return FALSE;
}

void ClientsController::addClient_main_thread(const char *hostName)
{
    const char *host_name = g_strdup(hostName);
    g_idle_add(addClient_main_thread, (gpointer) host_name);
}


Did you know that there is a C++ binding for GTK, it is called gtkmm. It may be a little late for you to use it for this application.

_________________
E.


Top
 Profile  
 
 Post subject: Re: Issue with gtk_widget_show
PostPosted: Mon Feb 10, 2014 9:02 am 
Offline

Joined: Sat Feb 08, 2014 9:59 am
Posts: 3
Yes thank you for your answer that's what I found on the net to solve the problem,
Now everything works, thank you for your help!
And I didn't know that there was a binding for c++ it's good to know it, maybe I could change my app.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: Exabot [Bot], Google Adsense [Bot], Yahoo [Bot] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group