Re: Program craches, why?



Le dimanche 25 septembre 2005 à 16:17 +0000, jalkadir gosonic ca a
écrit :
> I tried to provide as little code as possible, but to make what my
> intentions are clearer I had to add quite a bit, please bear with me…
> 
> This program runs, but when click on any button, with the exception of the
> “Exit” button, I get a segmentation fault error. I really don’t know why,
> but what I want to do is display a message-box with some data when the
> user presses any of the buttons, aside from the “Exit” button.
> 
> Can anyone help?
> 
> 
> TIA
> 
> -- messae.hpp --
> #include <string>
> #include <iostream>
> #include <gtk/gtk.h>
> #ifndef GTK_MESSAGE_HPP
> #define GTK_MESSAGE_HPP
> typedef struct{
>       gchar* str;
>       GtkWidget* obj;
>    }DATA;
> void GeneralMsg(GtkButton* button, gpointer d);
> 
> #endif
> -- end of file --
> 
> -- message.cpp --
> #ifndef GTK_MESSAGE_HPP
> #include "Message.hpp"
> #endif
> void GeneralMsg(GtkButton* button, gpointer p) {
>     DATA* d;
>     reinterpret_cast<DATA*>(p);

May be this should be:
	d = reinterpret_cast<DATA*>(p);

> 	GtkWidget* label;
> 	GtkWidget* dialog;
> 	try {
> 		dialog = new GtkWidget;
> 		label = new GtkWidget;
> 		d = new DATA;

You allocate data, but do not fill it.

> 		} catch(std::bad_alloc& e) {
> 		std::cout << e.what() << std::endl;
> 		}
> 
> 	dialog = gtk_message_dialog_new(
>                  reinterpret_cast<GtkWindow*>(d->obj),  //Parent

d->obj has not been initialized !

>                  static_cast<GtkDialogFlags>(
>                                 GTK_DIALOG_MODAL |
>                                 GTK_DIALOG_DESTROY_WITH_PARENT),//Flags
> 	         reinterpret_cast<GtkMessageType>(
>                                 GTK_MESSAGE_WARNING), // Message type
> 	         reinterpret_cast<GtkButtonsType>(
>                                 GTK_BUTTONS_OK),      //Button type
> 	         d->str);                              //Message format

d->str not initialized either.

> 
> // Destroy the dialog when the user responds to it (e.g. clicks a button)
> 	gtk_dialog_run (GTK_DIALOG (dialog));
> 	gtk_widget_destroy (dialog);
>         delete d;
> 	delete dialog;
> 	delete label;
> }
> 
> -- eof --
> 
> --- main.hpp ---
> //Declare the objects
>    GtkWidget* MainMenueWin;
>    GtkWidget* btnCreateAccount;
>    .....
> 
>    DATA* data;
> 
>    void Quit(GtkButton* , gpointer);
>    gboolean SystemQuit(GtkWidget*, GdkEvent*, gpointer);
> 
> //Callbacks
> void Quit(GtkButton* was_clicked, gpointer user_data){
>    gtk_main_quit();
> }
> gboolean SystemQuit(GtkWidget* widget, GdkEvent* event, gpointer user_data){
>      gtk_widget_destroy(widget);
>      gtk_main_quit();
>      return(TRUE);
> }
> #endif
> -- end of main.hpp --
> 
> 
> --- main.cpp ---
> #include <gtk/gtk.h>
> #include <glib-object.h>
> #include <string>
> #include <iostream>
> 
> #ifndef GTK_MESSAGE_HPP
>    #include "Message.hpp"
> #endif
> #include "main.hpp"
> 
> int main(int argc, char *argv[]){
>    try{data = new DATA;}
>    catch(std::bad_alloc& e){std::cout << e.what() << std::endl;}
>    //Assign data to the object passased to the callback function
>    data->str = g_strdup("Under Comstruction ... I am very sorry, but this \
>                     feature has not been implemented yet. Please check \
>                     my email me for any new updates on this program \
>                     El-Mago hotmail com");
>    data->obj = MainMenueWin;
> 
>    // 1. Initialize the environment
>    gtk_set_locale ();
>    gtk_init(&argc, &argv);
>    //-liconv -liconv
>    // 2. Create Widgets
>    MainMenueWin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
>    VBx = gtk_vbox_new(FALSE,10);
>    Label = gtk_label_new("Jaime");
>    Separator = gtk_hseparator_new();
>    btnCreateAccount = gtk_button_new_with_label("Create");
>    ...
>    btn5 = gtk_button_new_with_label("Unknown");
>    btnExit = gtk_button_new_with_label("Exit");
> 
>    // 3. Set attributs
>    gtk_window_set_title(GTK_WINDOW(MainMenueWin), "Jaime");
>    gtk_window_set_policy(GTK_WINDOW(MainMenueWin), //which_window
>                          FALSE,                    // allow_shrink
>                          FALSE,                    // allow_grow
>                          FALSE);                   // auto_shrink
> 
>    gtk_container_set_border_width(GTK_CONTAINER(MainMenueWin),15);
>    gtk_widget_set_name(MainMenueWin,"Gtk++ One, Dos, Tres");
>    gtk_widget_set_name(VBx,"vertical box");
>    gtk_widget_set_name(Label,"Label");
>    gtk_widget_set_name(Separator, "Separator");
> 
>    // 4. Register CallBacks
>    // These three callback fun' are only to exit the application
>    g_signal_connect( GTK_OBJECT(btnExit), "clicked",
>                      G_CALLBACK(Quit),    NULL);
>    g_signal_connect( GTK_OBJECT(MainMenueWin), "delete-event",
>                      GTK_SIGNAL_FUNC(SystemQuit), NULL);
>    g_signal_connect(GTK_OBJECT(MainMenueWin), "destroy",
>                     G_CALLBACK(SystemQuit), NULL);
> 
> 
>    g_signal_connect(GTK_OBJECT(btnCreateAccount),
>                     "clicked",
>                     G_CALLBACK(GeneralMsg),
>                     reinterpret_cast<gpointer>(&data));
> 
> 
>    return 0;
> }
> 
> 
> _______________________________________________
> gtk-list mailing list
> gtk-list gnome org
> http://mail.gnome.org/mailman/listinfo/gtk-list
> 




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