简体   繁体   中英

Avoiding Memory Allocation related errors

I have a complex C code with me and while executing it, I chanced upon the following errors:

  1. glibc: corrupted double-linked list
  2. glibc: malloc() memory corruption
  3. munmap_chunk() invalid pointer

I realized 1) is associated with freeing already freed memory. I am still trying to figure out the reasons for 2) and 3).

Well, the thing is then I did some searches and got the general opinion that I must debug with "valgrind" to detect memory corruption related problems.

Ok, coming back to the point,when I searched this forum, I have just dug up some code posted at: What is the best way to free memory after returning from an error?

And this piece of code had solved my problems:

int func(void **mem1, void **mem2)
{
    *mem1 = NULL;
    *mem2 = NULL;

    *mem1 = malloc(SIZE);
    if(!*mem1)
        goto err;

    *mem2 = malloc(SIZE);
    if(!*mem2)
        goto err;

    return 0;
err:
    if(*mem1)
        free(*mem1);
    if(*mem2)
        free(*mem2);

    *mem1 = *mem2 = NULL;

    return 1;
}

Well what really solved my issue is the line:

eg:

char *ptr = NULL;

ptr = (char *)malloc(SIZE);

assign and use ptr

free(ptr);

How is char *ptr = NULL helping???? Infact when I assigned to NULL in the beginning, I didn't even use free(ptr). It still worked liked a charm(I tried executing several times)

When I remove the NULL assignment in the beginning I get error 1):(:(

I am going to install Valgrind but before that I would like some insights on this.

Thanks

I'll take a shot in the dark and guess that you attempt to free() the ptr pointer before allocating with malloc() .

If it has been initialized to NULL , most free() implementations do nothing. From the free() manual page:

free() frees the memory space pointed to by ptr, which must have been returned by a previous call to malloc(), calloc() or realloc(). Otherwise, or if free(ptr) has already been called before, undefined behaviour occurs. If ptr is NULL, no operation is performed.

If it has not been set to NULL, you are trying to free either a random pointer, or something that has been already been freed.

That said, Valgrind is the best tool to properly detect such errors on POSIX systems.

EDIT:

What needs to be understood it that C is not Java and it does not have the luxuries of a VM. Everything exists within the same address space, with minimal protections - and that includes the structures of the memory allocator. Once a memory-related error occurs, there is no way to predict how it will make itself known.

Of the other two errors, I'd guess at first glance that (3) is once again related to freeing an address that has not been allocated. There is no way, however, to be sure that this is actually the issue. Once the memory of a process is corrupted, you cannot trust anything it tells you about itself - which is what's happening in this case.

Just use proper debugging tools like GDB or Valgrind and save yourself (and us) the pain of guessing blindly...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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