我正在用 gtk+ 2.0 和 gtksourceview 2.0 编写一个文本编辑器作为学习 C 的方式。当我尝试打开一个恰好位于 sshfs 上的文件时,我遇到了段错误。这并不总是一个问题,所以我不知道 sshfs 是否与它有关。
这是我的 open 函数中的一段代码:
printf("\nentered open_activated\n");
GtkSourceLanguage *lang;
GtkSourceLanguageManager *lm;
GtkWidget *dialog;
GtkWidget *tablabel;
GtkTextBuffer *tbuffer;
int openTabs = 0;
const gchar *folder;
int page = 0;
char *path,*string;
page = gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook));
path = paths[notebookPages[page]];
folder = folderFromPath(path);
printf("folder: %s\n\n",folder);
dialog = gtk_file_chooser_dialog_new("Open File", parent, GTK_FILE_CHOOSER_ACTION_OPEN,GTK_STOCK_CANCEL,GTK_RESPONSE_CANCEL,GTK_STOCK_OPEN,GTK_RESPONSE_ACCEPT,NULL);
gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER(dialog), folder);
附加信息
notebookPages 是一个全局整数数组:
#define NUM_TABS 100
int notebookPages[NUM_TABS];
路径是一个全局字符数组:
char paths[NUM_TABS][200];
这是函数folderFromPath:
char * folderFromPath(char *path)
{
printf("\nentered folderFromPath\n");
char *token[80];
int i = 0;
char str[80];
strcpy(str,path);
printf("path: %s\n", path);
token[0] = strtok(str, "/");
while (token[i]!= NULL)
{
i++;
token[i] = strtok (NULL, "/");
}
// char folder[128];
char *folder;
folder = malloc(sizeof(path));
if (folder != NULL)
{
int j = 0;
strcpy(folder,"/");
while (j < (i-1))
{
strcat(folder,token[j]);
strcat(folder,"/");
j++;
}
printf("folder: %s\n", folder);
return folder;
free(folder);
}
else
{
return "/";
}
}
这是来自 gdb 的回溯:
#0 0xb6e1d257 in g_slice_alloc () from /usr/lib/libglib-2.0.so.0
#1 0xb6e1e42e in g_slist_prepend () from /usr/lib/libglib-2.0.so.0
#2 0xb6e27c74 in g_once_init_enter_impl () from /usr/lib/libglib-2.0.so.0
#3 0xb739e24e in gtk_dialog_get_type () from /usr/lib/libgtk-x11-2.0.so.0
#4 0xb73d1527 in gtk_file_chooser_dialog_get_type () from /usr/lib/libgtk-x11-2.0.so.0
#5 0xb73d15d6 in ?? () from /usr/lib/libgtk-x11-2.0.so.0
#6 0xb73d1735 in gtk_file_chooser_dialog_new () from /usr/lib/libgtk-x11-2.0.so.0
#7 0x08050506 in open_activated (widget=0x8266878, parent=0x8083a60) at ledit.c:1715
#8 0xb6eea3dc in g_cclosure_marshal_VOID__VOID () from /usr/lib/libgobject-2.0.so.0
#9 0xb6ee8f5a in g_closure_invoke () from /usr/lib/libgobject-2.0.so.0
#10 0xb6efc816 in ?? () from /usr/lib/libgobject-2.0.so.0
#11 0xb6f05168 in g_signal_emit_valist () from /usr/lib/libgobject-2.0.so.0
#12 0xb6f05302 in g_signal_emit () from /usr/lib/libgobject-2.0.so.0
#13 0xb755a3af in ?? () from /usr/lib/libgtk-x11-2.0.so.0
#14 0xb6ee8f5a in g_closure_invoke () from /usr/lib/libgobject-2.0.so.0
#15 0xb6efc816 in ?? () from /usr/lib/libgobject-2.0.so.0
#16 0xb6f04f13 in g_signal_emit_valist () from /usr/lib/libgobject-2.0.so.0
#17 0xb6f05302 in g_signal_emit () from /usr/lib/libgobject-2.0.so.0
#18 0xb734aaa1 in gtk_accel_group_activate () from /usr/lib/libgtk-x11-2.0.so.0
#19 0xb734bf22 in gtk_accel_groups_activate () from /usr/lib/libgtk-x11-2.0.so.0
#20 0xb7575e7d in gtk_window_activate_key () from /usr/lib/libgtk-x11-2.0.so.0
#21 0xb7575eec in ?? () from /usr/lib/libgtk-x11-2.0.so.0
#22 0xb7427b44 in ?? () from /usr/lib/libgtk-x11-2.0.so.0
#23 0xb6ee7a37 in ?? () from /usr/lib/libgobject-2.0.so.0
#24 0xb6ee8f5a in g_closure_invoke () from /usr/lib/libgobject-2.0.so.0
#25 0xb6efc62c in ?? () from /usr/lib/libgobject-2.0.so.0
#26 0xb6f04f13 in g_signal_emit_valist () from /usr/lib/libgobject-2.0.so.0
#27 0xb6f05302 in g_signal_emit () from /usr/lib/libgobject-2.0.so.0
#28 0xb755bbc6 in ?? () from /usr/lib/libgtk-x11-2.0.so.0
#29 0xb7425d63 in gtk_propagate_event () from /usr/lib/libgtk-x11-2.0.so.0
#30 0xb742608f in gtk_main_do_event () from /usr/lib/libgtk-x11-2.0.so.0
#31 0xb72aa42a in ?? () from /usr/lib/libgdk-x11-2.0.so.0
#32 0xb6dfed42 in g_main_context_dispatch () from /usr/lib/libglib-2.0.so.0
#33 0xb6dff508 in ?? () from /usr/lib/libglib-2.0.so.0
#34 0xb6dffabb in g_main_loop_run () from /usr/lib/libglib-2.0.so.0
#35 0xb7424d59 in gtk_main () from /usr/lib/libgtk-x11-2.0.so.0
#36 0x0804d636 in main (argc=1, argv=0xbfad3774) at ledit.c:492
我正处于学习阶段,我知道我真的需要了解更好的内存管理和指针。关于可能导致此段错误的任何想法?或者关于如何进一步排除故障的任何想法?谢谢。