Note that there are some explanatory texts on larger screens.

plurals
  1. PODestructing Glib::RefPtr causes failed assertions in the GTK 3 core
    primarykey
    data
    text
    <p>The guys from Gtkmm are <a href="http://developer.gnome.org/gtkmm-tutorial/3.2/chapter-refptr.html.en" rel="noreferrer">comparing</a> <code>Glib::RefPtr</code> with <code>std::auto_ptr&lt;&gt;</code>:</p> <blockquote> <p><code>Glib::RefPtr</code> is a smartpointer. Specifically, it is a reference-counting smartpointer. You might be familiar with <code>std::auto_ptr&lt;&gt;</code>, which is also a smartpointer, but <code>Glib::RefPtr&lt;&gt;</code> is much simpler, and more useful.</p> </blockquote> <p>But for some strange reason, I can't get my work done with the <code>RefPtr</code>. The same code is just fine with a <code>auto_ptr</code>.</p> <p>In the following code, <code>SmartPtr</code> is just a placeholder for one of these two smartpointers.</p> <pre><code>#include &lt;gtkmm.h&gt; #include &lt;iostream&gt; #include &lt;tr1/memory&gt; struct WindowHolder { SmartPtr&lt;Gtk::Window&gt; ptr; WindowHolder() : ptr(new Gtk::Window) { ptr-&gt;signal_delete_event().connect(sigc::mem_fun(*this, &amp;WindowHolder::reset)); ptr-&gt;show_all(); } bool reset(GdkEventAny* event) { Gtk::Main::quit(); } }; int main(int argc, char *argv[]) { Gtk::Main kit(argc, argv); WindowHolder w; kit.run(); } </code></pre> <p>When compiling, I first define <code>SmartPtr</code> as <code>Glib::RefPtr</code> and then as <code>std::auto_ptr</code>.</p> <pre><code>$ g++ '-DSmartPtr=Glib::RefPtr' `pkg-config --cflags --libs gtkmm-3.0` main.cc &amp;&amp; ./a.out (main:22093): GLib-GObject-CRITICAL **: g_object_unref: assertion `G_IS_OBJECT (object)' failed $ g++ '-DSmartPtr=std::auto_ptr' `pkg-config --cflags --libs gtkmm-3.0` main.cc &amp;&amp; ./a.out $ </code></pre> <p>The problem is this <code>GLib-GObject-CRITICAL</code>. In my real application, this isn't just a single line but a whole bunch of them. In the second version with <code>std::auto_ptr</code> everything gets destructed well.</p> <p>Strange enough the code it is just fine in GTK 2:</p> <pre><code>$ g++ '-DSmartPtr=Glib::RefPtr' `pkg-config --cflags --libs gtkmm-2.4` main.cc &amp;&amp; ./a.out $ </code></pre> <p>I don't want to depend on <code>std::auto_ptr</code> because it is deprecated and I also don't want to work with a raw pointer because then the destructors have to manually delete the pointers which adds extra complexity ...</p> <p>My questions are:</p> <ol> <li>Why causes <code>Glib::RefPtr</code> this "critical warning" (probably a double free)?</li> <li>Why does it work with gtkmm 2.4 but not in 3.0?</li> <li>Can I fix the code with <code>Glib::RefPtr</code> and gtkmm 3.0?</li> <li>How should I handle such situations in general?</li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload