【发布时间】:2013-04-14 12:22:21
【问题描述】:
现状:
我有一个带有事件窗口的自定义小部件 (MyWidget)。
问题:如果我创建、显示然后隐藏和销毁小部件,我会从应用程序收到以下消息:
Gdk-WARNING **: losing last reference to undestroyed window
我发现了什么:我查看了gdkwindow.c 文件,当GDK_WINDOW_DESTROYED(window) == FALSE 时报告了此消息。所以我不明白的是我应该如何正确地销毁我的窗口,以便最终调用gdk_window_destroy() 函数。我认为调用它的最佳位置是 Gdk::~Window() 析构函数。但它是空的。而且gdk_window_destroy() 在gdkwindow.cc 文件中根本不存在。
on_realize() 和 on_unrealize() 回调如下。
class MyWidget : public Gtk::Widget
{
...
private:
Glib::RefPtr<Gdk::Window> _event_window;
...
};
void Gtk::MyWidget::on_realize()
{
GdkWindowAttr attributes;
const Allocation & allocation = get_allocation();
attributes.event_mask = GDK_BUTTON_PRESS_MASK;
attributes.x = allocation.get_x();
attributes.y = allocation.get_y();
attributes.width = allocation.get_width();
attributes.height = allocation.get_height();
attributes.wclass = GDK_INPUT_ONLY;
attributes.window_type = GDK_WINDOW_CHILD;
_event_window = Gdk::Window::create(get_parent_window(), &attributes, GDK_WA_X | GDK_WA_Y);
_event_window->set_user_data(Widget::gobj());
set_window(get_parent_window());
set_realized();
}
void Gtk::MyWidget::on_unrealize()
{
_event_window->set_user_data(NULL);
_event_window.reset();
set_realized(false);
}
【问题讨论】: