Double free problem

How to solve a double free problem by writing a wrapper function without a name so that I don't change every free call in my source code?

+3
source share
6 answers

Do not do that

An easy way is to define your wrapper function and #define to call your function.

#undef free
#define free(x) wrapper_free(x)
/* ... */
int *data = malloc(42);
free(data); /* effectively calls wrapper_free(data) */

but ... don't do this!

+8
source

Do not do this.

No really. Correct the actual problem. After free () has been called with a pointer, your code should not hold onto it for any reason. Kill him so you cannot release him again; it will also cause any other problems caused by visual dereferencing of pointers.

+28
source

malloc(), realloc() calloc() .

free() , . , . , .

memdebug.h:

#undef free
#define free(PTR) memdebug_free(PTR, #PTR, __FILE__, __func__, __LINE__)

#undef malloc
#define malloc(SIZE) memdebug_log(malloc(SIZE))

#undef realloc
#define realloc(PTR, SIZE) memdebug_log(realloc(PTR, SIZE))

#undef calloc
#define calloc(COUNT, SIZE) memdebug_log(calloc(COUNT, SIZE))

#ifndef MEMDEBUG_H
#define MEMDEBUG_H

extern void memdebug_free(void *ptr, const char *ptr_arg, const char *file, 
    const char *func, int line);
extern void *memdebug_log(void *ptr);

#endif

memdebug.c:

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

#ifndef MEMDEBUG_TABLE_SIZE
// log 16k allocations by default
#define MEMDEBUG_TABLE_SIZE 0x4000
#endif

static void *alloc_table[MEMDEBUG_TABLE_SIZE];
static size_t top;

void *memdebug_log(void *ptr)
{
    assert(top < sizeof alloc_table / sizeof *alloc_table);
    alloc_table[top++] = ptr;
    return ptr;
}

void memdebug_free(void *ptr, const char *ptr_arg, const char *file,
    const char *func, int line)
{
    if(!ptr)
    {
        fprintf(stderr,
            "%s() in %s, line %i: freeing null pointer `%s` -->continue\n",
            func, file, line, ptr_arg);

        return;
    }

    for(size_t i = top; i--; )
    {
        if(ptr == alloc_table[i])
        {
            free(ptr);
            --top;
            if(i != top) alloc_table[i] = alloc_table[top];
            return;
        }
    }

    fprintf(stderr,
        "%s() in %s, line %i: freeing invalid pointer `%s`-->exit\n",
        func, file, line, ptr_arg);

    exit(EXIT_FAILURE);
}
+7

, , . , - , ( ).

, , , ( , ).

#define FREEANDCLEAR(pointer)\
{\
   free(pointer);\
   pointer = 0;\
}

, , ( , do while:

#define FREEANDCLEAR(pointer)\
do {\
   free(pointer);\
   pointer = 0;\
} while(0)

.

+1

..

, , , ..

Java 10 , Java, /.

( C/++), open/close, create/destroy, lock/unlock, malloc/free, , , .

, , , , . , , .

, , , , .

+1

Check out the answers to the stackoverflow question, "Write your own memory manager . "

Using tools or methods in these answers will allow you to include a memory manager that checks for these types of problems so that you can identify and fix them (as many other answers to your question indicate that it is not recommended to use something as a workaround).

0
source

Source: https://habr.com/ru/post/1717932/


All Articles