GtkTextBuffer 问题,令人困惑的运行时错误。需要帮助吗?
我正在使用此代码:
class editbook
{
GtkWidget* _nbook;
std::vector<GtkWidget*> _srcset; //and so on...
........................................................ ………………………………
void editbook::add_page()
{
GtkWidget* tmp = gtk_source_view_new();
_srcset.push_back(tmp);
gtk_notebook_append_page(GTK_NOTEBOOK(_nbook),tmp,gtk_label_new("untitled"));
}
...................................................... ......................................
void editbook::set_text(const std::string& text)
{
int index = gtk_notebook_get_current_page(GTK_NOTEBOOK(_nbook));
GtkTextBuffer* tbuffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(_srcset[index]));
gtk_text_buffer_set_text(GTK_TEXT_BUFFER(tbuffer),text.c_str(),-1);
}
编译良好。但给出了这个奇怪的运行时错误:
Segementation Failure: return 139
我已将问题追溯到: gtk_text_view_get_buffer(GTK_TEXT_VIEW(_srcset[index]));
注意:我使用 GtkSourceView 而不是 GtkTextView,但那可能不是问题,因为当我尝试 GtkTextView 时,我遇到了同样的错误。
注意:我正在使用 Gtk 2x
注意:我不确定是否用 C 或 C++ 标记这个问题。因为Gtk+ 是一个 C 库。但我用的是C++。所以我现在只标记两者。
I am using this code:
class editbook
{
GtkWidget* _nbook;
std::vector<GtkWidget*> _srcset; //and so on...
...........................................................................................
void editbook::add_page()
{
GtkWidget* tmp = gtk_source_view_new();
_srcset.push_back(tmp);
gtk_notebook_append_page(GTK_NOTEBOOK(_nbook),tmp,gtk_label_new("untitled"));
}
...........................................................................................
void editbook::set_text(const std::string& text)
{
int index = gtk_notebook_get_current_page(GTK_NOTEBOOK(_nbook));
GtkTextBuffer* tbuffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(_srcset[index]));
gtk_text_buffer_set_text(GTK_TEXT_BUFFER(tbuffer),text.c_str(),-1);
}
Compiles fine. But gives this weird runtime error:
Segementation Fault: return 139
I have traced down the problem to: gtk_text_view_get_buffer(GTK_TEXT_VIEW(_srcset[index]));
NOTE: I am using GtkSourceView instead of GtkTextView, but that may not be a problem because I am gettin the same error when I try GtkTextView.
NOTE: I am using Gtk 2x
NOTE: I am not sure whether to tag this question with C or C++. bec. Gtk+ is a C lib. But I am using C++. So I'll just tag both for now.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您代码中的问题可能是通过
gtk_notebook_append_page
添加到GtkNotebook
的子小部件不可见,请尝试通过显示子小部件gtk_widget_show
调用。这些行中的内容:当您使用
gtk_notebook_get_current_page
时,如果没有任何子窗口小部件可见,则它返回-1
,我认为这可能会发生在您的情况下 &因为当您使用operator[]
时,index
为-1
,它不会检查程序崩溃的边界。我强烈建议您使用vector::at
而不是使用operator[]
,以便您在运行时收到std::out_of_range
异常来指示问题。您可以使用:希望这有帮助!
The problem in your code could be that the child widget added to
GtkNotebook
throughgtk_notebook_append_page
is not visible, try showing the child widget throughgtk_widget_show
call. Something on these lines :When you use
gtk_notebook_get_current_page
if none of the child widget are visible then it returns-1
, which I think might be happening in your case & asindex
is-1
when you useoperator[]
which doesn't check for bounds the program crashes. I strongly suggest you usevector::at
instead of usingoperator[]
so that you getstd::out_of_range
exception during run time to indicate the problem. You could use:Hope this helps!