Question

I have a program that tries to update a Gtk::Label at a very high frequency, and is exhibing very unstable behaviour. I get several of these errors:

(gtkWindow:26559): Pango-CRITICAL **: pango_layout_copy: assertion 'PANGO_IS_LAYOUT (src)' failed

(gtkWindow:26559): Pango-CRITICAL **: pango_layout_set_width: assertion 'layout != NULL' failed

(gtkWindow:26559): Pango-CRITICAL **: pango_layout_get_pixel_extents: assertion 'PANGO_IS_LAYOUT (layout)' failed

(gtkWindow:26559): GLib-GObject-CRITICAL **: g_object_unref: assertion 'G_IS_OBJECT (object)' failed

until the program eventually crashes with:

Pango:ERROR:pango-layout.c:3916:pango_layout_check_lines: assertion failed: (!layout->log_attrs)

2921 Aborted (core dumped)

The relevant code lines:

while(1){
    std::string sensorLine="";
    _serial.readLine(&sensorLine);         // read serial data with boost::asio
    _output->set_label(sensorLine.data()); // _output -> Gtk::Label*
    std::cout<<sensorLine<<std::endl;
    //sleep(1);
}

I only get the error if I try to use _output->setlabel, if I comment this line everything runs smoothly, with the output printed in the console. The same thing happens if I call sleep()inside the loop, the Gtk::Label is updated as the commandline and no errors are thrown. This loop is running on a separate thread that receives _output as argument.

Was it helpful?

Solution

Use g_idle_add (which actually is threadsafe) with a callback which in turn actually modifies (read: calls set_label on) your GtkLabel.


Do not call UI functions from a different thread! Never ever! You open the box of pandora if you do.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top