GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Thu May 23, 2013 2:06 pm

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: GTK+ segmentation fault with text view widget
PostPosted: Tue Sep 20, 2011 6:19 am 
Offline

Joined: Tue Sep 20, 2011 6:06 am
Posts: 1
Hello,

I am new to GTK+ and have had problems printing to the text view buffer. Below is a piece of code which appears to have heap issues in the "go_build" function. Is there a problem with my implementation of the text view box or is this an issue within the GTK lib?

Note: If you remove the "int i" declaration after GtkTextBuffer *buffer and GtkTextIter *iter the code runs without a problem.

Thanks


#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>

static GtkWidget *status_message_area;

void init_user_window();
void go_build(GtkWidget *button);

int main(int argc, char** argv) {

gtk_init(&argc, &argv);
init_user_window();
gtk_main();

return (EXIT_SUCCESS);
}

void init_user_window()
{
GtkWidget *window;
GtkWidget *vbox1;
GtkWidget *vbox2;
GtkWidget *hbox1;
GtkWidget *build_button;
GtkWidget *exit_button;
GtkWidget *frame;
GtkWidget *scrolled_window1;

window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Hello");
g_signal_connect(G_OBJECT(window), "destroy",
G_CALLBACK (gtk_widget_destroyed), NULL);
g_signal_connect(G_OBJECT(window), "delete_event",
G_CALLBACK(gtk_main_quit), NULL);

vbox1 = gtk_vbox_new(FALSE, 0);
gtk_container_add(GTK_CONTAINER(window), vbox1);

hbox1 = gtk_hbox_new(TRUE, 0);
gtk_box_pack_start(GTK_BOX(vbox1), hbox1, TRUE, TRUE, 15);

vbox2 = gtk_vbox_new(FALSE, 0);
gtk_box_pack_start(GTK_BOX(hbox1), vbox2, TRUE, TRUE, 30);

/*
* Add Exit Button
*/
exit_button = gtk_button_new_with_label("Exit");
gtk_box_pack_start(GTK_BOX(vbox2), exit_button, TRUE, TRUE, 0);
g_signal_connect(exit_button, "clicked",
G_CALLBACK (gtk_main_quit), NULL);
/*
* Add Build Button
*/
build_button = gtk_button_new_with_label("Build");
gtk_box_pack_start(GTK_BOX(vbox2), build_button, TRUE, TRUE, 0);
g_signal_connect(build_button, "clicked",
G_CALLBACK (go_build), NULL);
/*
* Add the status text area at the bottom of the window
*/
frame = gtk_frame_new("Status:");
gtk_widget_set_usize(frame, 480, 100);
gtk_box_pack_start(GTK_BOX(vbox1), frame, TRUE, TRUE, 0);
gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_OUT);

scrolled_window1 = gtk_scrolled_window_new(NULL, NULL);
gtk_container_add(GTK_CONTAINER(frame), scrolled_window1);
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window1),
GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);

status_message_area = gtk_text_view_new();
gtk_container_add(GTK_CONTAINER(scrolled_window1), status_message_area);
gtk_text_view_set_editable(GTK_TEXT_VIEW(status_message_area), FALSE);
gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(status_message_area),
GTK_WRAP_WORD);

gtk_widget_show_all(window);
}


void go_build(GtkWidget *button)
{
const gchar *status_message = "Building...\n";

GtkTextBuffer *buffer;
GtkTextIter *iter;
int i;

buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(status_message_area));
gtk_text_buffer_get_end_iter(buffer, iter);
gtk_text_buffer_insert(buffer, iter, status_message, -1);
}



Top
 Profile  
 
 Post subject: Re: GTK+ segmentation fault with text view widget
PostPosted: Tue Sep 20, 2011 8:15 am 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 510
Location: UK
Hi,

Yes you are right that you have heap problems with your code. Your function go_build should look like this :-
Code:
void go_build(GtkWidget *button)
{
    const gchar *status_message = "Building...\n";

    GtkTextBuffer *buffer;
    GtkTextIter iter;

    buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(status_message_area));
    gtk_text_buffer_get_end_iter(buffer, &iter);
    gtk_text_buffer_insert(buffer, &iter, status_message, -1);
}
Note the way GtkTextIter is used, it is created on the heap and not a pointer. Your previous code would have had a pointer that pointed to an unknown area of memory.

Also gtk_widget_set_usize () has been deprecated since GTK+ version 2.2 (which is now very old) and you should use gtk_widget_set_size_request () instead.

Errol.

_________________
E.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group