简体   繁体   English

使用calloc和free后内存泄漏?

[英]Memory leak after using calloc and free?

I tested my software with "valgrind --leak-check=full", and it shows: 我用“ valgrind --leak-check = full”测试了我的软件,它显示:

==90862== 7,627 bytes in 4 blocks are definitely lost in loss record 858 of 897
==90862==    at 0x4C2FB55: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==90862==    by 0xD64991C: concat(int, ...) (Client.cpp:150)

I can't understand why, because I use free() after calloc. 我不明白为什么,因为我在calloc之后使用free()。 Here's my code: 这是我的代码:

char* p = concat(2, buffOld, buff);
char *x;
    while(true) {
        x = p;
        p = strstr(p,"\\final\\");
        if(p == NULL) { break; }
        *p = 0;
        p+=7;
        parseIncoming((char *)x,strlen(x));
    }
free(p);

And the "concat" function: 和“ concat”功能:

char* concat(int count, ...)
{
    va_list ap;
    int i;

    // Find required length to store merged string
    int len = 1; // room for NULL
    va_start(ap, count);
    for(i=0 ; i<count ; i++)
        len += strlen(va_arg(ap, char*));
    va_end(ap);

    // Allocate memory to concat strings
    char *merged = (char*)calloc(sizeof(char),len);
    int null_pos = 0;

    // Actually concatenate strings
    va_start(ap, count);
    for(i=0 ; i<count ; i++)
    {
        char *s = va_arg(ap, char*);
        strcpy(merged+null_pos, s);
        null_pos += strlen(s);
    }
    va_end(ap);

    return merged;
}

What I do wrong? 我做错了什么?

I can't understand why, because I use free() after calloc 我不明白为什么,因为我在calloc之后使用free()

Yes, but (if I understand correctly) you free() the wrong pointer. 是的,但是(如果我理解正确),您free()了错误的指针。

You should copy p in another pointer (before modifing it) and free() the save copy. 您应该将p复制到另一个指针中(在对其进行修改之前),并使用free()保存副本。

Look at your code 看你的代码

char* p = concat(2, buffOld, buff);
char *x;
    while(true) {
        x = p;
        p = strstr(p,"\\final\\");
        if(p == NULL) { break; }
        *p = 0;
        p+=7;
        parseIncoming((char *)x,strlen(x));
    }
free(p);

The pointer p is initialized with the calloc-ed pointer but the while cicle modify it and return only when p is NULL . 指针p已使用calloc-ed指针初始化, while cicle对其进行了修改,并且仅在pNULL时返回。

So, when you call 所以,当你打电话

free(p)

you're calling 你在打电话

free(nullptr)

--- EDIT --- -编辑-

I still don't understand it. 我还是不明白。 I added free(x) at the end, and it crashes 我在最后添加了free(x),它崩溃了

My initial suggestion to free(x) was a mistake of mine because I didn't pointed the attention to the fact that x is initializes with the p value but is modified in the while loop. 我最初对free(x)建议是我的一个错误,因为我没有将注意力集中在xp值初始化但在while循环中修改的事实。 Thanks again to Johnny Mopp for pointing my attention to it. 再次感谢约翰尼·莫普(Johnny Mopp)引起我的注意。

I suggest the use of another variable to memorize the original value of p (the exact value returned by calloc() ) and free this value. 我建议使用另一个变量来记住p的原始值(由calloc()返回的确切值)并释放该值。

Something like 就像是

char* p = concat(2, buffOld, buff);
char *x;
char * const  forFree = p; /* <--- saving calloc() returned value */

while(true) {
    x = p;
    p = strstr(p,"\\final\\");
    if(p == NULL) { break; }
    *p = 0;
    p+=7;
    parseIncoming((char *)x,strlen(x));
}

free(forFree);

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

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