views:

4594

answers:

7

Hey, I am trying to create wrapper functions for free and malloc in C to help notify me of memory leaks. Does anyone know how to declare these functions so when I call malloc() and free() it will call my custom functions and not the standards lib functions?

+2  A: 

If you define your own functions for malloc() and free() and explicitly link that with your applications, your functions should be used in preference to those in the library.

However, your function called 'malloc' cannot then call the library malloc function, because in 'c' there's no concept of separate namespaces. In other words, you'd have to implement the internals of malloc and free yourself.

Another approach would be to write functions my_malloc() and my_free(), which call the standard library ones. This would mean that any code calling malloc would have to be changed to call your my_xxx functions.

Roddy
And you can #define malloc my_malloc to get your code to work without modification. But you'll need to be consistent about usage - don't use my_malloc if the memory will be freed in a library, or vice versa.
Mark Ransom
+21  A: 

If your compilation environment is glibc with gcc, the preferred way is to use malloc hooks. Not only it lets you specify custom malloc and free, but will also identify the caller by the return address on the stack.

Alternatively, you can define malloc and free as wrappers to the original allocation routines in your application, which will "override" the version from libc. Inside the wrapper you can call into the original malloc implementation, which you can look up using dlsym with RTLD_NEXT handle:

#define _GNU_SOURCE
#include <dlfcn.h>
#include <stdio.h>

void* malloc(size_t sz) {
    void *(*libc_malloc)(size_t) = dlsym(RTLD_NEXT, "malloc");
    printf("malloc\n");
    return libc_malloc(sz);
}

void free(void *p) {
    void (*libc_free)(void*) = dlsym(RTLD_NEXT, "free");
    printf("free\n");
    libc_free(p);
}

int
main()
{
    free(malloc(10));
    return 0;
}

In the latter case, your application or library that defines wrapper functions needs to link with -ldl.

Alex B
+1  A: 

If your goal is to eliminate memory leaks, an easier, less intrusive way is to use a tool like Valgrind (free) or Purify (costly).

Don Wakefield
A: 

If you are using Linux, you can use malloc_hook() (with GNU glibc). This function allows you to have malloc call your function prior to calling the actual malloc. The man page has an example on how to use it.

+2  A: 

In C, the method I used was similar to:

#define malloc(x) _my_malloc(x, __FILE__, __LINE__)
#define free(x) _my_free(x)

This allowed me to detect the line and file of where the memory was allocated without too much difficulty. It should be cross-platform, but will encounter problems if the macro is already defined (which should only be the case if you are using another memory leak detector.)

If you want to implement the same in C++, the procedure is a bit more complex but uses the same trick.

Raymond Martineau
Best not to use leading underscores in names - they're mainly reserved to the implementation.
Jonathan Leffler
Right, he's using those values as defined in the implementation. http://gcc.gnu.org/onlinedocs/gcc-3.2.3/cpp/Standard-Predefined-Macros.html
bradtgmurray
A: 

If you are only talk about memory that you have under control, i.e. that you malloc and free on your own, you can take a look on rmdebug. Probably it is what you are going to write anyway, so you can save sometime. It has a very liberal licence, if that should be important for you.

I personally use it in a project, to look for memory leaks, the nice things is that it is much faster then valgrind, however it isn't that powerful so you don't get the full calling stack.

quinmars
+2  A: 

Here's a set of wrapper functions I used for years (and still do when I dip into C) to detect unfree'd memory, memory free'd multiple times, references to free'd memory, buffer overflows/underflows, and freeing memory that was not allocated.

ftp://ftp.digitalmars.com/ctools.zip

They've been around for 25 years and have proven themselves.

You could use the macro preprocessor to redefine malloc and free to use the mem package ones, but I recommend against it, because it won't redirect library calls to malloc like what strdup does.

Walter Bright