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.
The problem in your code could be that the child widget added to
GtkNotebookthroughgtk_notebook_append_pageis not visible, try showing the child widget throughgtk_widget_showcall. Something on these lines :When you use
gtk_notebook_get_current_pageif none of the child widget are visible then it returns-1, which I think might be happening in your case & asindexis-1when you useoperator[]which doesn’t check for bounds the program crashes. I strongly suggest you usevector::atinstead of usingoperator[]so that you getstd::out_of_rangeexception during run time to indicate the problem. You could use:Hope this helps!