Search code examples
linuxsegmentation-faultg++gtkmm

Segfault (-11) on program exit


I'm trying to get goocanvasmm working from a C++ program. From a couple of places I combined code snippets and got the program to draw.

However, on program termination, a segfault appears. I'm not a normal C++ user, and as such, the gdb messages are somewhat cryptic to me.

Here's the code. It creates a window, and draws a 200x200 pixel yellow square in it:

#include <gtkmm.h>
#include <goocanvasmm.h>
#include <iostream>
#define UI_FILE "gtk_foobar.ui"

int
main (int argc, char *argv[]) {
  Gtk::Main kit(argc, argv);

  //Load the Glade file and instiate its widgets:
  Glib::RefPtr<Gtk::Builder> builder;
  try {
    builder = Gtk::Builder::create_from_file(UI_FILE);
  }
  catch (const Glib::FileError & ex) {
    std::cerr << ex.what() << std::endl;
    return 1;
  }
  
  Gtk::Window *main_win = 0;
  builder->get_widget("main_window", main_win);
  
  Goocanvas::Canvas canvas;
  
  main_win->add(canvas);
  auto root = (canvas).get_root_item();
  auto rect = Goocanvas::Rect::create(100, 100, 200, 200);
  
  rect->property_stroke_color() = "yellow";
  rect->property_line_width() = 3;
  root->add_child(rect);
  
  main_win->show_all();

  if (main_win) {
    kit.run(*main_win);
  }
  return 0;
}

the run-time error message (shown on program termination):

Program has been terminated receiving signal 11 (Segmentation fault)

and the gdb backtrace:

#0  0x00007ffff5ae7d6c in free () at /lib64/libc.so.6
#1  0x00007ffff5a97736 in __cxa_finalize () at /lib64/libc.so.6
#2  0x00007ffff7d22946 in __do_global_dtors_aux () at /usr/lib64/libgoocanvasmm-2.0.so.6
#3  0x00007ffff7ffd060 in _rtld_local () at /lib64/ld-linux-x86-64.so.2
#4  0x0000000000000000 in  ()

If I'm not mistaken, this seems to indicate a problem with the destructors in libgoocanvasmm.

Edit: I realized (a little late) that, to run the application, it's also necessary to have the UIFILE. Here it is ("gtk_foobar.ui"):

<?xml version="1.0"?>
<interface>
  <!-- interface-naming-policy project-wide -->
  <!-- interface-requires gtk+ 3.0 -->
  <object class="GtkWindow" id="main_window">
    <property name="visible">True</property>
    <property name="title" translatable="yes">Hello World!</property>
    <property name="default_width">500</property>
    <property name="default_height">400</property>
    <child>
      <placeholder/>
    </child>
  </object>
</interface>

To make things easier, this link will download the complete autotools project.


Solution

  • Thanks to @BobMorane who tested the program and found a minor version difference between libraries. It seems that both my source package and his are labeled 2.0.6, but internally there's a minor difference, 1.90.9 and 1.90.11, which was important.

    Recompiling with the 1.90.11 version solved the problem!

    Checking the Changelog showed no world-shocking differences, but a series of minor compatibility updates. Re-running valgrind did show the disappearance of this message:

    ==30082== Invalid free() / delete / delete[] / realloc()
    ==30082==    at 0x483B71B: operator delete(void*) (vg_replace_malloc.c:802)
    ==30082==    by 0x6C25735: __cxa_finalize (in /lib64/libc-2.30.so)
    ==30082==    by 0x48FB945: ??? (in /usr/lib64/libgoocanvasmm-2.0.so.6.0.0)
    ==30082==    by 0x4914FC0: ??? (in /usr/lib64/libgoocanvasmm-2.0.so.6.0.0)
    ==30082==    by 0x6C25016: __run_exit_handlers (in /lib64/libc-2.30.so)
    ==30082==    by 0x6C251C9: exit (in /lib64/libc-2.30.so)
    ==30082==    by 0x6C03E61: (below main) (in /lib64/libc-2.30.so)
    ==30082==  Address 0x8 is not stack'd, malloc'd or (recently) free'd