简体   繁体   English

在C程序中找到崩溃的来源

[英]find the source of crash in C program

I have a code base where the process running on linux is crashing in free and sometimes in malloc: 我有一个代码库,在linux上运行的进程在免​​费崩溃,有时在malloc中崩溃:

#0  0xffffe430 in __kernel_vsyscall ()
#1  0xf7424e30 in raise () from /lib/libc.so.6
#2  0xf7426765 in abort () from /lib/libc.so.6
#3  0xf7469d33 in malloc_printerr () from /lib/libc.so.6
#4  0xf746e7bc in free () from /lib/libc.so.6
#5  0xf6047e25 in myFree (mem_ptr=0x82165d8) at ../my_code/mylib.c:78
#6  0xf6014a10 in FreeBuffer (buffer=0x82042f8)

In the code I am not seeing anything fishy at the place where memory freeing is happening. 在代码中,我没有在发生内存释放的地方看到任何可疑的东西。 Function myFree() has nothing but a free() function call. 函数myFree()只有一个free()函数调用。

void FreeBuffer(struct MY_BUFFER *buffer)
{
    if (buffer)
    {
        myFree(buffer);
    }
}


void myFree(void *mem_ptr)
{

        free(mem_ptr);
}

I tried using MALLOC_CHECK_ but it was not of any help. 我尝试使用MALLOC_CHECK_但它没有任何帮助。

I suspect some where the heap is getting corrupted and want to find that. 我怀疑一些堆已经损坏并希望找到它。 Any hint to proceed to debug the process in such cases? 在这种情况下是否有任何提示继续调试过程?

There is hard to be 100% sure but most probably you are trying to free memory twice. 很难100%肯定,但最有可能的是你试图释放记忆两次。 It's great that you are checking if pointer is not NULL before trying to free it 在尝试释放指针之前检查指针是否为NULL非常棒

if (buffer)
    {
        myFree(buffer);
    }

but you are probably NOT setting pointer NULL after doing exact free job - that's my guess. 但是你可能没有在做完确的免费工作后设置指针NULL - 这是我的猜测。

Check if you have something like this 检查你是否有这样的东西

struct MY_BUFFER *buffer;
//do something with buffer
FreeBuffer(buffer)
buffer = NULL;

You can do this as well inside FreeBuffer() function but to do so you will have to pass address of pointer AKA pointer to pointer so the definition will be like this FreeBuffer(struct MY_BUFFER **buffer) 你也可以在FreeBuffer()函数中执行此操作但是为了这样做你必须将指针AKA指针的地址传递给指针,这样定义就像这个FreeBuffer(struct MY_BUFFER **buffer)

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

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