Valgrind C中的free()/ delete / delete [] / realloc()无效

时间:2016-04-23 20:18:53

标签: c memory-leaks malloc valgrind free

Valgrind告诉我,内存中有泄漏,我试图释放()但是我认为它没有正确完成。有任何想法吗?谢谢。

  

无效的free()/ delete / delete [] / realloc()   在0x4C27D4E:free(vg_replace_malloc.c:427)

     

by 0x400C00:main(main.c:149)

     

地址0x51ba138在大小为8的块

后为0字节      

在0x4C28BED:malloc(vg_replace_malloc.c:263)   通过0x400B0E:main(main.c:119)

     

HEAP SUMMARY:   在退出时使用:1个块中的2个字节   总堆使用量:5个分配,5个释放,14个字节分配

     

1个块中的2个字节肯定会丢失1个丢失记录1

     

at 0x4C28BED:malloc(vg_replace_malloc.c:263)

     

by 0x40084F:strdup(main.c:19)

     

by 0x4009C4:permute(main.c:83)

     

by 0x400B9C:main(main.c:138)

char *strdup (const char *s)
{
    char *d = malloc (strlen (s) + 1);  // Space for length plus null //line 19
    if (d == NULL) {
        return NULL;            // No memory
    }
    strcpy (d, s);              // Copy the characters
    return d;                   // Return the new string
}

void permute (char *arrayOfPermutations, int startIndex, int stopIndex,
            char ***permuts)
{
    int i;
    if (startIndex == stopIndex) {
        **permuts = strdup (arrayOfPermutations);       //save generated string //line 83
        *permuts += 1;          //increment location
    } else {
        for (i = startIndex; i <= stopIndex; i++) {
            swap ((arrayOfPermutations + startIndex),
                (arrayOfPermutations + i));
            permute (arrayOfPermutations, startIndex + 1, stopIndex, permuts);
            swap ((arrayOfPermutations + startIndex),
                (arrayOfPermutations + i));
        }
    }
}

int main (int argc, char *argv[])
{
    char *stringInput, c = 0;
    unsigned int j = 0, i = 0, stringSize, facto;
    char **permuts, **work;

    stringInput = (char *) malloc (sizeof (char));

    while (c != '\n') {
        c = getc (stdin);       //read the input from keyboard standard input
        stringInput = (char *) realloc (stringInput, (j + 1) * sizeof (char));  //re-allocate (resize) memory for character read to be stored
        stringInput[j] = c;     //store read character by making pointer point to c
        j++;
    }

    stringInput[j - 1] = '\0';  //add null termination
    stringSize = strlen (stringInput);
    facto = factorial (stringSize);

    permuts = (char **) malloc (facto * sizeof (char *));       // allocate n! pointers //line 119
    work = permuts;

    printf ("String size: %d\n", stringSize);

    ...some printfs here...permute (stringInput, 0, stringSize - 1, &work);     //creates permutations of chars //line 138

    qsort (permuts, facto, sizeof (char *), compare);   //sorts strings alphabetically

    for (i = 0; i <= facto - 1; i++) {
        printf ("\"%s\"\n", permuts[i]);
    }

    free (work);                //free the memory //line 149
    free (permuts);             //free the memory
    free (stringInput);         //free the memory
}

2 个答案:

答案 0 :(得分:1)

在其他问题中,您的代码的主要问题是:

double free or corruption (out): 0x000000000074a060 ***

引起:

char **permuts, **work;
...
work = permuts;
...
free (work);                //free the memory //line 149
free (permuts);             //free the memory

你正在释放相同的内存块两次。

答案 1 :(得分:0)

在我阅读你的代码之前。 Valgrind报告无效的免费错误不是因为泄漏的内存,而是你试图释放无效的内存(或多次释放相同的内存)。使用compilter flag -g(gdb debug)编译代码将启用更多的调试信息,使得valgrind的回溯更好。

快速猜测:

  ..
  for(i = 0; i <= facto-1; i++)
   {
    printf("\"%s\"\n", permuts[i]);
    free (permuts[i]); /* free the text, allocated with strdup(); */
  }

  free(permuts);//free the memory
  free(stringInput);//free the memory
}