0
的Structs:段錯誤在C/GTK + 3程序的標題內定義的
struct info{
gint mode, isSignalActivate;
gchar *filename;
GtkWidget *pwdEntry, *pwdReEntry, *mainwin, *dialog, *file_dialog;
};
extern struct info s_Info;
struct hashes{
gchar *filename;
GtkWidget *entryMD5, *entryS1, *entryS256, *entryS512, *entryWhir, *entryRMD;
GtkWidget *checkMD5, *checkS1, *checkS256, *checkS512, *checkWhir, *checkRMD;
};
extern struct hashes s_HashType;
和節目的主要部分:
int main(int argc, char **argv){
if(!gcry_check_version(GCRYPT_MIN_VER)){
fputs("libgcrypt min version required: 1.5.0\n", stderr);
return -1;
}
gcry_control(GCRYCTL_INIT_SECMEM, 16384, 0);
gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
GtkApplication *app;
int status;
app = gtk_application_new ("org.gtk.polcrypt",G_APPLICATION_FLAGS_NONE);
g_signal_connect (app, "startup", G_CALLBACK (startup), NULL);
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
return status;
}
static void startup (GtkApplication *application, gpointer user_data __attribute__ ((unused)))
{
static const GActionEntry actions[] = {
{ "about", about },
{ "quit", quit }
};
GMenu *menu;
g_action_map_add_action_entries (G_ACTION_MAP (application), actions, G_N_ELEMENTS (actions), application);
menu = g_menu_new();
g_menu_append (menu, "About", "app.about");
g_menu_append (menu, "Quit", "app.quit");
gtk_application_set_app_menu (application, G_MENU_MODEL (menu));
g_object_unref (menu);
}
static void activate (GtkApplication *app, gpointer user_data __attribute__ ((unused)))
{
GtkWidget *butHa, *grid;
struct info s_Info;
s_Info.mainwin = gtk_application_window_new(app);
gtk_window_set_application (GTK_WINDOW (s_Info.mainwin), GTK_APPLICATION (app));
gtk_window_set_position(GTK_WINDOW(s_Info.mainwin), GTK_WIN_POS_CENTER);
gtk_window_set_title(GTK_WINDOW(s_Info.mainwin), "PolCrypt");
gtk_window_set_resizable(GTK_WINDOW(s_Info.mainwin), FALSE);
gtk_container_set_border_width(GTK_CONTAINER(s_Info.mainwin), 10);
butHa = gtk_button_new_with_label("Compute Hash");
g_signal_connect(butHa, "clicked", G_CALLBACK (is_hash), &s_Info);
grid = gtk_grid_new();
gtk_container_add(GTK_CONTAINER(s_Info.mainwin), grid);
gtk_grid_set_row_homogeneous(GTK_GRID(grid), TRUE);
gtk_grid_set_column_homogeneous(GTK_GRID(grid), TRUE);
gtk_grid_set_row_spacing(GTK_GRID(grid), 5);
gtk_grid_set_column_spacing(GTK_GRID(grid), 5);
//numero colonna, numero riga, colonne da occupare, righe da occupare. Colonne e righe sono aggiunte automaticamente
gtk_grid_attach(GTK_GRID(grid), butHa, 0, 0, 5, 1);
gtk_widget_show_all(s_Info.mainwin);
}
static void is_hash(GtkWidget *ignored __attribute__ ((unused)), struct info *s_Info){
s_Info->mode = 3;
file_dialog(s_Info);
}
static void file_dialog(struct info *s_Info){
s_Info->file_dialog = gtk_file_chooser_dialog_new("Choose File", NULL, GTK_FILE_CHOOSER_ACTION_OPEN, ("_Cancel"), GTK_RESPONSE_CANCEL, ("_Ok"), GTK_RESPONSE_ACCEPT, NULL);
if (gtk_dialog_run (GTK_DIALOG (s_Info->file_dialog)) == GTK_RESPONSE_ACCEPT){
s_Info->filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (s_Info->file_dialog));
if(s_Info->mode == 3){
select_hash_type(s_Info);
g_free (s_Info->filename);
}
}
gtk_widget_destroy (s_Info->file_dialog);
}
static void select_hash_type(struct info *s_InfoHash){
gtk_widget_hide(GTK_WIDGET(s_InfoHash->file_dialog));
struct hashes s_HashType;
GtkWidget *content_area, *grid2;
s_InfoHash->dialog = gtk_dialog_new_with_buttons ("Select Hash", NULL, GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT, "_Quit", GTK_RESPONSE_CLOSE, NULL);
content_area = gtk_dialog_get_content_area (GTK_DIALOG (s_InfoHash->dialog));
s_HashType.checkMD5 = gtk_check_button_new_with_label("MD5");
s_HashType.entryMD5 = gtk_entry_new();
gtk_editable_set_editable(GTK_EDITABLE(s_HashType.entryMD5), FALSE);
gtk_widget_set_size_request(s_InfoHash->dialog, 250, 150); // richiedo una grandezza minima
grid2 = gtk_grid_new();
gtk_grid_set_row_homogeneous(GTK_GRID(grid2), TRUE); // righe stessa altezza
gtk_grid_set_column_homogeneous(GTK_GRID(grid2), TRUE); // colonne stessa larghezza
gtk_grid_set_row_spacing(GTK_GRID(grid2), 5); // spazio fra le righe
// numero colonna, numero riga, colonne da occupare, righe da occupare
gtk_grid_attach(GTK_GRID(grid2), s_HashType.checkMD5, 0, 0, 1, 1);
gtk_grid_attach(GTK_GRID(grid2), s_HashType.entryMD5, 2, 0, 6, 1);
/* Add the grid, and show everything we've added to the dialog */
gtk_container_add (GTK_CONTAINER (content_area), grid2);
gtk_widget_show_all (s_InfoHash->dialog);
s_HashType.filename = malloc(strlen(s_InfoHash->filename)+1);
strcpy(s_HashType.filename, s_InfoHash->filename);
g_signal_connect_swapped(s_HashType.checkMD5, "clicked", G_CALLBACK(compute_md5), &s_HashType);
gint result = gtk_dialog_run(GTK_DIALOG(s_InfoHash->dialog));
switch(result){
case GTK_RESPONSE_CLOSE:
gtk_widget_destroy(s_InfoHash->dialog);
break;
}
free(s_HashType.filename);
}
我的問題是在當點擊該程序段錯誤「退出」按鈕s_InfoHash->dialog
。
上面是gdb的輸出。
Program received signal SIGSEGV, Segmentation fault.
0x00000000009aa4f0 in ??()
(gdb) where
#0 0x00000000009aa4f0 in ??()
#1 0x00000000008f2290 in ??()
#2 0x00007fffffffd330 in ??()
#3 0x00000000007e0480 in ??()
#4 0x0000000000000188 in ??()
#5 0x0000000040000002 in ??()
#6 0x00007ffff5afbba9 in g_hash_table_lookup()
from /usr/lib64/libglib-2.0.so.0
#7 0x00007ffff5e17b7f in g_signal_emit_valist()
from /usr/lib64/libgobject-2.0.so.0
#8 0x00007ffff5e1881a in g_signal_emit() from /usr/lib64/libgobject-2.0.so.0
#9 0x00007ffff76be454 in ??() from /usr/lib64/libgtk-3.so.0
#10 0x00007ffff75943dc in ??() from /usr/lib64/libgtk-3.so.0
#11 0x00007ffff7595c2a in gtk_main_do_event() from /usr/lib64/libgtk-3.so.0
#12 0x00007ffff71ac522 in ??() from /usr/lib64/libgdk-3.so.0
#13 0x00007ffff5b0bc66 in g_main_context_dispatch()
from /usr/lib64/libglib-2.0.so.0
#14 0x00007ffff5b0bfb8 in ??() from /usr/lib64/libglib-2.0.so.0
#15 0x00007ffff5b0c05c in g_main_context_iteration()
from /usr/lib64/libglib-2.0.so.0
#16 0x00007ffff60d9eec in g_application_run() from /usr/lib64/libgio-2.0.so.0
#17 0x0000000000403339 in main (argc=1, argv=0x7fffffffd878)
at src/gtk3/main-gui.c:35
這件事情讓我發瘋了,因爲我無法找到是什麼原因造成的段錯誤...(我知道,GDB說gtk_application_run (#17)
但這是什麼意味着什麼?我該如何調試和修復這個東西?)
我使用的GCC 4.8.1和GTK + 3.10
問題是'struct info s_Info;'在'activate'函數內聲明。我在外面宣佈它,現在所有的工作都完美:) – polslinux