c++ - Gdk::Pixmap 在 create() 上给我一个段错误

标签 c++ gdk

我正在 Gtk::DrawingArea 上绘制像素。有时会发生公开事件,我需要重绘所有内容。但是再次绘制每个像素很慢,所以我尝试创建一个 Sdk::Pixmap 并绘制到它,然后在需要时将它绘制到屏幕上。

我正在尝试创建一个 Sdk::Pixmap 并使用 draw_point() 绘制它。这是我的代码: 在 hpp 中:

Glib::RefPtr<Gdk::Pixmap>pixmap;
Glib::RefPtr<Gdk::GC>pixGC;

在 cpp 中:

Glib::RefPtr<Gdk::Drawable> d = Gdk::Drawable::create(); // this has a value of 1
pixmap = Gdk::Pixmap::create(d, width, height, 3); // this call is giving me a seg fault
pixGC = Gdk::GC::create();

绘制时:

pixmap->draw_point(pixGC,x, y); // don't even know if this works yet because of the seg fault

我的代码有什么问题? :(

此外,我还没有编写代码来绘制到实际的 DrawingArea。

编辑:好的,我修复了第一个段错误:

pixmap = Gdk::Pixmap::create(get_window(), width, height, 3);
pixGC = Gdk::GC::create(get_window());

感谢这个链接: http://marc.info/?l=gtkmm&m=108547746915009

但现在我明白了:

The program 'programName' received an X Window System error.
This probably reflects a bug in the program.
The error was 'BadDrawable (invalid Pixmap or Window parameter)'.
(Details: serial 69525 error_code 9 request_code 64 minor_code 0)
(Note to programmers: normally, X errors are reported asynchronously;
that is, you will receive the error a while after causing it.
To debug your program, run it with the --sync command line
option to change this behavior. You can then get a meaningful
backtrace from your debugger if you break on the gdk_x_error() function.)

堆栈跟踪:

Breakpoint 3, 0x00007ffff72830e0 in _XError ()
   from /usr/lib/x86_64-linux-gnu/libX11.so.6
(gdb) bt
#0  0x00007ffff72830e0 in _XError () from /usr/lib/x86_64-linux-gnu/libX11.so.6
#1  0x00007ffff72801d1 in ?? () from /usr/lib/x86_64-linux-gnu/libX11.so.6
#2  0x00007ffff7280215 in ?? () from /usr/lib/x86_64-linux-gnu/libX11.so.6
#3  0x00007ffff7281050 in _XReply () from /usr/lib/x86_64-linux-gnu/libX11.so.6
#4  0x00007ffff727c99d in XSync () from /usr/lib/x86_64-linux-gnu/libX11.so.6
#5  0x00007ffff727ca2b in ?? () from /usr/lib/x86_64-linux-gnu/libX11.so.6
#6  0x00007ffff725f954 in XCreatePixmap ()
   from /usr/lib/x86_64-linux-gnu/libX11.so.6
#7  0x00007ffff3e2f48d in ?? ()
   from /usr/lib/x86_64-linux-gnu/libgdk-x11-2.0.so.0
#8  0x00007ffff6de048d in Gdk::Pixmap::Pixmap (this=0x7fffd4000930, 
    drawable=..., width=256, height=256, depth=3, __in_chrg=<optimized out>, 
    __vtt_parm=<optimized out>) at pixmap.cc:55
#9  0x00007ffff6de0d71 in Gdk::Pixmap::create (drawable=..., width=256, 
    height=256, depth=<optimized out>) at pixmap.cc:317
#10 0x00000000004408e1 in Viewer::start (this=0x7fffffffea00, width=256, 
    height=256) at viewer.cpp:23
#11 0x000000000042e380 in AppWindow::rayTraceDrawer (this=0x7fffffffe8e0)
    at appwindow.cpp:25
#12 0x0000000000432cd8 in std::_Mem_fn<void (AppWindow::*)()>::operator() (
    this=0x767b58, __object=0x7fffffffe8e0)
    at /usr/include/c++/4.6/functional:551
#13 0x0000000000432b8b in std::_Bind_result<void, std::_Mem_fn<void (AppWindow::---Type <return> to continue, or q <return> to quit---
*)()> (AppWindow*)>::__call<void, , 0>(std::tuple<>&&, std::_Index_tuple<0>, std::_Bind_result<void, std::_Mem_fn<void (AppWindow::*)()> (AppWindow*)>::__enable_if_void<void>::type) (this=0x767b58, __args=...)
    at /usr/include/c++/4.6/functional:1287
#14 0x0000000000432ae3 in std::_Bind_result<void, std::_Mem_fn<void (AppWindow::*)()> (AppWindow*)>::operator()<>() (this=0x767b58)
    at /usr/include/c++/4.6/functional:1378
#15 0x0000000000432972 in std::thread::_Impl<std::_Bind_result<void, std::_Mem_fn<void (AppWindow::*)()> (AppWindow*)> >::_M_run() (this=0x767b40)
    at /usr/include/c++/4.6/thread:117
#16 0x00007ffff6031c78 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#17 0x00007ffff589de9a in start_thread ()
   from /lib/x86_64-linux-gnu/libpthread.so.0
#18 0x00007ffff55ca3fd in clone () from /lib/x86_64-linux-gnu/libc.so.6
#19 0x0000000000000000 in ?? ()

这是因为我在后台线程上创建像素图吗?

编辑 2:

不,移至 UI 线程但仍然崩溃...

编辑 3:

看来我可能已经修复了它。问题是过早访问 get_window() 。将代码移至 on_realize 并停止崩溃。不确定像素图是否有效……我会在弄清楚如何从像素图绘制到绘图区后发布。

最佳答案

这么多小时后的解决方案是:

在你的 DrawingArea 的 on_realize 中:

pixmap = Gdk::Pixmap::create(get_window(), width, height, get_window()->get_depth());
pixGC = Gdk::GC::create(get_window());

绘制到像素图:

pixGC->set_foreground(color);
pixmap->draw_point(pixGC,x,y);

然后绘制到DrawingArea:

get_window()->draw_drawable(get_style()->get_fg_gc(get_state()),
                    pixmap,
                    0,0,0,0);

关于c++ - Gdk::Pixmap 在 create() 上给我一个段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22432169/

相关文章:

c - 如何将光标运动信号连接到函数 (GTK)

c - 如何获取 Gtk2 GtkDrawingArea 的 XID 来嵌入 Mplayer

c++ - 在 C++ 中包含来自单独文件夹的文件

c++ - 指向类方法错误的指针数组 C++11

windows - 有没有办法让 SendInput 与使用 GDK 的应用程序一起工作?

c++ - gdk_screen_get_default() 返回 null

c - 使用 Xlib 函数在 GtkDrawingArea 上绘图

c++ - 模板元编程 Eigen 表达式

c++ - 想要的CWMP CPE(客户端)和ACS(服务器)

c++ - openMP 嵌套并行 for 循环与内部并行 for