在CairoContext上创建smartpointer时出现Segfault
Segfault when creating smartpointer on CairoContext
在Cairo- context上创建Cairo::RefPtr时遇到了一些问题。我真的无法想象为什么这个段会出错,除了指针ist指向完全错误的东西。
这是我的代码。
int main(int argc, char * argv[])
{
Gtk::Main kit(argc, argv);
Gtk::Window window;
Gtk::DrawingArea drawarea;
window.add(drawarea);
Cairo::RefPtr<Cairo::Context> ccontext = drawarea.get_window()->create_cairo_context();
Gtk::Allocation allocation = drawarea.get_allocation();
const int width = allocation.get_width();
const int height = allocation.get_height();
ccontext->set_source_rgb(1.0, 0.0, 0.0);
ccontext->set_line_width(2.0);
ccontext->move_to(0,0);
ccontext->line_to(width, height);
Gtk::Main::run(window);
}
GDB是这么说的:
启动程序:/home/marian/desktop/c++/Langton/Langton[使用libthread_db调试]
程序收到信号SIGSEGV,分段错误。0 xb7be852eGdk::Window::create_cairo_context() () from/usr/lib/libgdkmm-3.0.so.1
我用gcc (gcc) 4.6.1 20110819 (pre - release)编译了这个。
Thanks in advance
Gtk::Widget::get_window()返回null Glib::RefPtr,因为小部件还没有实现。
根据GtkDrawingArea文档,您需要挂钩到"draw"信号来处理绘图,在这里您的Cairo上下文已经创建并交给了您。回到Gtkmm参考,您可以使用Gtk::Widget::signal_draw()来挂钩它,或者您可以重载虚拟的on_draw()函数来处理您的绘图。
此外,你还需要在每个小部件上调用。show(),即你的DrawingArea和你的Window,并调用ccontext->stroke()来获得实际绘制的线。
结果看起来像这样:
#include <gtkmm.h>
bool draw (const Cairo::RefPtr<Cairo::Context> &ccontext, Gtk::DrawingArea *drawarea)
{
Gtk::Allocation allocation = drawarea->get_allocation();
const int width = allocation.get_width();
const int height = allocation.get_height();
ccontext->set_source_rgb(1.0, 0.0, 0.0);
ccontext->set_line_width(2.0);
ccontext->move_to(0,0);
ccontext->line_to(width, height);
ccontext->stroke ();
return true;
}
int main(int argc, char * argv[])
{
Gtk::Main kit(argc, argv);
Gtk::Window window;
Gtk::DrawingArea drawarea;
drawarea.signal_draw ().connect (sigc::bind (sigc::ptr_fun (&draw),
&drawarea));
window.add(drawarea);
window.show_all ();
Gtk::Main::run(window);
return 0;
}
或者:
#include <gtkmm.h>
class LineBox : public Gtk::DrawingArea
{
protected:
virtual bool on_draw (const Cairo::RefPtr<Cairo::Context> &ccontext);
};
bool LineBox::on_draw (const Cairo::RefPtr<Cairo::Context> &ccontext)
{
Gtk::Allocation allocation = get_allocation();
const int width = allocation.get_width();
const int height = allocation.get_height();
ccontext->set_source_rgb(1.0, 0.0, 0.0);
ccontext->set_line_width(2.0);
ccontext->move_to(0,0);
ccontext->line_to(width, height);
ccontext->stroke ();
return true;
}
int main(int argc, char * argv[])
{
Gtk::Main kit(argc, argv);
Gtk::Window window;
LineBox drawarea;
window.add(drawarea);
window.show_all ();
Gtk::Main::run(window);
return 0;
}
相关文章:
- C++中带有List类的迭代器Segfault
- 使用Vulkan hpp vk::enumerateInstanceVersion()会导致segfault
- SegFault 同时使用 std::string::operator+= 和函数作为参数
- std::partition segfault issue
- OpenSSL: EC_POINT_set_compressed_coordinates_GFp segfault
- 检查nullptr是否100%保护内存布局不受segfault影响
- OpenCV Tracker 属性访问在 ARM 上因 SEGFAULT 而失败,但在 X86_64 中工作
- 为什么优化大型 std::vector 数组会导致 SegFault?
- C++segfault,可重复的例子
- 带有zip_source_buffer的libzip会导致数据损坏和/或segfault
- 导致SegFault C++的析构函数
- 从引用的Vector获取SEGFAULT
- 尝试读取/写入Graphviz DAG值的工作证明会导致segfault
- Segfault如果更改派生类的指针值
- SegFault deleting QTreeWidgetItem
- PyImport_Import segfault
- 添加#pragma循环后出现Segfault
- 使用 gmock 时的 SegFault
- C 多线程,在并行多个线程时获得SegFault
- 使用std :: to_string()创建std :: String时的segfault