简体   繁体   English

何时/如何释放从函数返回的指针

[英]when/how to free pointer returned from function

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 . 在其中,您可以看到我已动态分配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. 主要问题可能是这个。

char * folderFromPath(char *path)
...
folder = malloc(sizeof(path));

You are allocating sizeof(path) , which is the same as sizeof(char*) , since path is a char* . 您正在分配sizeof(path) ,它与sizeof(char*) ,因为pathchar* You're only allocating enough bytes to hold a single pointer, rather than the whole path which is probably what you intended. 你只需要分配足够的字节来保存单个指针,而不是整个路径,这可能是你想要的。

Try instead: 尝试改为:

folder = malloc(strlen(path)+1);

There may well be other problems; 可能还有其他问题; I haven't looked very closely. 我没有仔细看过。 It does look as if you are correctly freeing the return folder after passing it to gtk_file_chooser_set_current_folder() , although I don't know why you are assigning it to folder2 as well. 在将其传递给gtk_file_chooser_set_current_folder()后,它看起来好像正确地释放了返回folder ,虽然我不知道为什么你也将它分配给folder2 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; 如果你期望复制字符串的赋值(GTK函数是否期望获取路径的所有权?)那么你会感到失望; you will have to take a separate copy of the string using strncpy() or something similar. 你将不得不使用strncpy()或类似的东西单独复制字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM