Skip to content

mulle-core/mulle-testallocator

Repository files navigation

mulle-testallocator

πŸ”„ C memory leak and double free checking

mulle-testallocator is a leak and double free checker for tests (and at runtime). It builds upon mulle-allocator.

Release Version Release Notes
Mulle kybernetiK tag Build Status RELEASENOTES

API

You can configure trace output and other properties of the Test Allocator via the API or environment variables.

Usage

Use mulle_testallocator for leak detection

Use mulle_malloc and friends instead of malloc in your code.

So instead of:

   malloc( 1848);
   calloc( 18, 48);
   s = strdup( "VfL Bochum 1848");
   realloc( s, 18);
   free( s);

write

   mulle_malloc( 1848);
   mulle_calloc( 18, 48);
   s = mulle_strdup( "VfL Bochum 1848");
   mulle_realloc( s, 18);
   mulle_free( s);

Now you can easily check for leaks using this mulle_testallocator library. Just run your code with the environment variable MULLE_TESTALLOCATOR set to YES. mulle-testallocator will tell you your leaks when the executable exits.

Note

This feature needs a C-compiler that handles __attribute__(((constructor)).

The order of constructor and atexit calls is dependent on the link order. To catch all leaks, it is advantageous to link mulle-testallocator ahead of all other code.

Manual method

Or you can wrap your code inside the following piece of code:

mulle_testallocator_initialize();
mulle_default_allocator = mulle_testallocator;
{
   mulle_malloc( 1848);
   mulle_calloc( 18, 48);
   s = mulle_strdup( "VfL Bochum 1848");
   mulle_realloc( s, 18);
   mulle_free( s);
}
mulle_testallocator_reset();

and mulle_testallocator_reset will tell you about your leaks. You don't need the 'constructor' support then.

All mulle_testallocator routines will check for erroneous frees and wrong pointers.

Tip

Locate Objective-C leaks easily with

MULLE_OBJC_PEDANTIC_EXIT=YES MULLE_TESTALLOCATOR=YES \
MULLE_OBJC_EPHEMERAL_SINGLETON=YES MULLE_OBJC_TRACE_INSTANCE=YES \
MULLE_OBJC_TRACE_METHOD_CALL=YES MULLE_TESTALLOCATOR_TRACE=2 \
   ./kitchen/Debug/myexe

Then search for the leak address and you will see the method that allocated the leak.

You are here

Overview

Add

You should use whole archive linking as otherwise the library may just be omitted from the link. (The mulle-sde mark all-load will do this for you).

Add as an individual component

Use mulle-sde to add mulle-testallocator to your project:

mulle-sde dependency add --marks all-load,no-singlephase \
                         --github mulle-core \
                         mulle-testallocator

To only add the sources of mulle-testallocator with dependency sources use clib:

clib install --out src/mulle-core mulle-core/mulle-testallocator

Add -isystem src/mulle-core to your CFLAGS and compile all the sources that were downloaded with your project.

Install

Install with mulle-sde

Use mulle-sde to build and install mulle-testallocator and all dependencies:

mulle-sde install --prefix /usr/local \
   https://github.com/mulle-core/mulle-testallocator/archive/latest.tar.gz

Manual Installation

Install the requirements:

Requirements Description
mulle-thread πŸ”  Cross-platform thread/mutex/tss/atomic operations in C
mulle-allocator πŸ”„ Flexible C memory allocation scheme
mulle-stacktrace πŸ‘£ Stracktrace support for various OS
mulle-atinit 🀱🏼 Compatibility library for deterministic initializers
mulle-atexit πŸ‘Ό Compatibility library to fix atexit
mulle-dlfcn ♿️ Shared library helper

Download the latest tar or zip archive and unpack it.

Install mulle-testallocator into /usr/local with cmake:

cmake -B build \
      -DCMAKE_INSTALL_PREFIX=/usr/local \
      -DCMAKE_PREFIX_PATH=/usr/local \
      -DCMAKE_BUILD_TYPE=Release &&
cmake --build build --config Release &&
cmake --install build --config Release

Author

Nat! for Mulle kybernetiK