Segmentation fault while reading from entry.



Hello.
I'm am new to gtk+ and I try to write a simple application, I need to
read a double from an entry, I've already written a parsing
*char->double function but I can't even read from the entry using
gtk_editable_get_chars because I get Segmentation Fault. I don't know
what is the reason for this, so I'll post parts of the code so that
you could help me in finding the bug:


calculate.c
===================

#include <glib.h>
#include <gtk/gtkwidget.h>
#include <gtk/gtkentry.h>

#include <stdio.h>

#include "calculate.h"
#include "main_window.h"

/* ... */

void calculate_cb(GtkWidget *widget, GdkEvent *event, gpointer data)
{
       double a = 0;
       gchar *s = NULL;
       MainWindow *main_window = (MainWindow *)data;

       /*
        *   segmentation fault here
        */
       s = gtk_editable_get_chars(GTK_EDITABLE(main_window->a_entry),
0, -1);

       if (parse_double(s, &a)) {
               DO_SOME_STUFF_WITH_IT(a);

       g_free((gpointer)s);
}

=====================
main_window.c
=====================

#include <glib.h>
/* ... */
#include <gtk/gtkmain.h>

#include "main_window.h"
#include "calculate.h"

/* ... */

MainWindow *main_window_create(void)
{
       MainWindow *main_window;

       main_window = g_malloc(sizeof(MainWindow));

       main_window->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

     /* ...... oter widgets, callbacks ................ */

       main_window->calc_btn = gtk_button_new_with_label("calculate");
       g_signal_connect(G_OBJECT(main_window->calc_btn), "clicked",
               G_CALLBACK(calculate_cb), main_window);

       main_window->a_entry = gtk_entry_new();
       gtk_entry_set_max_length(GTK_ENTRY(main_window->a_entry),
ENTRY_MAX_LEN);

      /* doesn't show main_window->window */
      show_all_widgets(&main_window);

      /* ..... putting in a table ............ */

       gtk_widget_show(main_window->window);

       return main_window;
}

================
main.c
================
#include <stdio.h>

#include <glib.h>
#include <gtk/gtkmain.h>

#include "main_window.h"

int main(int argc, char *argv[])
{
       MainWindow *main_window;
       gtk_init(&argc, &argv);
       main_window = main_window_create();
       gtk_main();
       return 0;
}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]