0

I have the following sample program (C++):

#include <iostream>
#include <gtkmm.h>
#include <boost/thread.hpp>

using namespace std;

Gtk::Window * window;
Glib::Dispatcher dispatcher;

void do_updates();
void load_layout();

int x;

int main(int argc, char ** argv)
{
    Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "nl.mycroes.test");

    window = new Gtk::Window;
    window->set_default_size(200, 200);

    Gtk::Label * label = new Gtk::Label("label");
    window->add(*label);

    dispatcher.connect(&load_layout);

    window->show_all();

    boost::thread t = boost::thread(do_updates);

    app->run(*window);

    Gtk::Widget * child = window->get_children().front();
    delete child;

    return 0;
}

void load_layout()
{
    stringstream layout;
    layout << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
           << endl << "<interface>"
           << endl << "<object class=\"GtkWindow\" id=\"window\">"
           << endl << "<child>"
           << endl << "<object class=\"GtkLabel\" id=\"text\">"
           << endl << "<property name=\"visible\">True</property>"
           << endl << "<property name=\"can_focus\">False</property>"
           << endl << "<property name=\"label\">" << x << "</property>"
           << endl << "</object>"
           << endl << "</child>"
           << endl << "</object>"
           << endl << "</interface>";

    Glib::RefPtr<Gtk::Builder> builder = Gtk::Builder::create_from_string(layout.str());
    Gtk::Window * win;
    builder->get_widget("window", win);

    std::vector<Gtk::Widget *> old_children = window->get_children();
    Gtk::Widget * child = old_children.front();

    window->remove();
    delete child;

    std::vector<Gtk::Widget *> new_children = win->get_children();
    child = new_children.front();
    child->reparent(*window);

    delete win;
}

void do_updates()
{
    for (x = 0; x < 10000; x++)
    {
        boost::this_thread::sleep(boost::posix_time::milliseconds(1));
        dispatcher();
    }
}

If I keep an eye on the memory usage (watch -n 1 "grep ^Vm /proc/\$(pgrep GtkLeakTest)/status") it keeps increasing, but I don't know what's staying around in memory. Also, valgrind doesn't report any leaks at all, which makes it seem like I'm properly cleaning everything up...

I actually tried if I had to recursive delete the window child, because this is a testcase for a program where I'm loading a larger galde layout and that also leaks, but I tried and and logically doesn't make a difference because I don't put nested components in the window. Any hints are appreciated!

mycroes
  • 645
  • 8
  • 20

0 Answers0