我有以下示例程序(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();
}
}
如果我密切关注内存使用量(watch -n 1 "grep ^Vm /proc/\$(pgrep GtkLeakTest)/status"
),它会不断增加,但我不知道内存中会留下什么。此外,valgrind根本没有报告任何泄漏,这使我觉得我正在清理所有东西......
我实际上尝试过,如果我必须递归删除窗口子项,因为这是一个程序的测试用例,我正在加载更大的galde布局并且也泄漏,但我尝试过并且逻辑上没有什么区别因为我没有在窗口中放置嵌套组件。任何提示都表示赞赏!