I have this function:
char * folderFromPath(char *path)
{
printf("\nentered folderFromPath\n");
char *token[80];
int i = 0;
const int STR_LEN = 128;
char str[STR_LEN];
char *folder;
folder = malloc(sizeof(path));
strcpy(folder,"/");
if (strlen(path) > STR_LEN)
{
printf("Warning: strlen(path) > STR_LEN, (%d > %d) in function folderFromPath\n", strlen(path), STR_LEN);
}
else
{
printf("path: %s\n", path);
strcpy(str,path);
token[0] = strtok(str, "/");
while (token[i]!= NULL)
{
i++;
token[i] = strtok (NULL, "/");
printf("token[i]: %s, i: %d\n", token[i], i);
}
if (folder != NULL)
{
int j = 0;
while (j < (i-1))
{
strcat(folder,token[j]);
strcat(folder,"/");
j++;
}
printf("folder: %s\n", folder);
}
} /* else if (strlen(path) < STR_LEN) */
return folder;
}
In it you can see that I have dynamically allocated memory that is pointed to by folder. You can also see that folder is returned to the calling function. I saw in this post where it was suggested to free the pointer after it is used in the calling function. So that is what I have done. Here is the calling function:
void open_activated(GtkWidget *widget, GtkWindow *parent)
{
GtkSourceLanguage *lang;
GtkSourceLanguageManager *lm;
GtkWidget *dialog;
GtkWidget *tablabel;
GtkTextBuffer *tbuffer;
int openTabs = 0;
char *folder1;
const gchar *folder2;
int page = 0;
char *path;
page = gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook));
path = paths[notebookPages[page]];
folder1 = folderFromPath(path);
folder2 = folder1;
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), folder2);
free(folder1);
tbuffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(txtinput[openedPages]));
gtk_source_buffer_begin_not_undoable_action(GTK_SOURCE_BUFFER(tbuffer));
if(gtk_dialog_run(GTK_DIALOG(dialog))== GTK_RESPONSE_ACCEPT)
{
...
} /* if(gtk_dialog_run(GTK_DIALOG(dialog))== GTK_RESPONSE_ACCEPT) */
gtk_widget_destroy(dialog);
changeLabelColor("black");
gtk_text_buffer_set_modified (gtk_text_view_get_buffer((GTK_TEXT_VIEW(txtinput[openedPages]))), FALSE);
gtk_source_buffer_end_not_undoable_action(GTK_SOURCE_BUFFER(tbuffer));
write_config_files();
verifyPaths();
}
When I try to open a file, the application aborts and produces this statement:
*** glibc detected *** ./ledit: free(): invalid next size (fast): 0x082a80c8 ***
So my question is what does this error mean and what should I do differently to properly free the pointer? Thanks.
The main problem is probably this.
You are allocating
sizeof(path), which is the same assizeof(char*), sincepathis achar*. You’re only allocating enough bytes to hold a single pointer, rather than the whole path which is probably what you intended.Try instead:
There may well be other problems; I haven’t looked very closely. It does look as if you are correctly freeing the return
folderafter passing it togtk_file_chooser_set_current_folder(), although I don’t know why you are assigning it tofolder2as well. If you are expecting that assignment to copy the string (does the GTK function expect to take ownership of the path?) then you will be disappointed; you will have to take a separate copy of the string usingstrncpy()or something similar.