0

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.

5
  • 1
    Could you post what is _srcset array? Btw why don't you use GtkMM ( gtkmm.org/en ) which is C++ wrapper over Gtk in case you want to write your code in C++? Commented Sep 22, 2011 at 10:28
  • @another.anon.coward std::vector<GtkWidget*> _srcset . Filled with GtkSourceViews (gtk_source_view_new()); As for GtkMM. Frankly I don't use it because I am an idiot... And because It doesn't compile on Ubuntu for some reason (glibmmconfig.h not found) Commented Sep 22, 2011 at 12:16
  • You're not showing enough information. Tell us where you define _srcset and why _srcset[index] must be a valid reference at the time of execution. Commented Sep 22, 2011 at 12:23
  • @sehe Now do you have enough info? Commented Sep 22, 2011 at 12:27
  • 1
    Hmmm ... Did you check if buffer returned for NULL & if they are valid objects? I mean what is the output for GTK_IS_TEXT_VIEW(_srcset[index]) & GTK_IS_TEXT_BUFFER(tbuffer)? Also, I would suggest you please use vector::at instead of operator[] just to be sure that you are not going out of the range. Commented Sep 22, 2011 at 12:35

1 Answer 1

1

The problem in your code could be that the child widget added to GtkNotebook through gtk_notebook_append_page is not visible, try showing the child widget through gtk_widget_show call. Something on these lines :

void editbook::add_page()
{
    GtkWidget* tmp = gtk_source_view_new();
    _srcset.push_back(tmp);
    gtk_widget_show(tmp); //Show the child widget to make it visible
    gtk_notebook_append_page(GTK_NOTEBOOK(_nbook),tmp,gtk_label_new("untitled"));
}

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 & as index is -1 when you use operator[] which doesn't check for bounds the program crashes. I strongly suggest you use vector::at instead of using operator[] so that you get std::out_of_range exception during run time to indicate the problem. You could use:

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.at(index)));
    gtk_text_buffer_set_text(GTK_TEXT_BUFFER(tbuffer),text.c_str(),-1);
}

Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.