Skip to content

Instantly share code, notes, and snippets.

@drrost
Created September 13, 2020 10:39
Show Gist options
  • Save drrost/173dd7971a472deb75c8f253d13e438a to your computer and use it in GitHub Desktop.
Save drrost/173dd7971a472deb75c8f253d13e438a to your computer and use it in GitHub Desktop.
Checking for leaks in C code
//
// Created by Rostyslav Druzhchenko on 05.09.2020.
//
#include <stdio.h>
#include <dlfcn.h>
int malloc_counter = 0;
// ================================== PUBLIC ==================================
void check_leaks() {
if (malloc_counter != 0)
fprintf(stderr, "LEAKS: there are %d leaks\n", malloc_counter);
}
// ================================= INTERNAL =================================
static void *(*real_malloc)(unsigned long) = 0;
static void (*real_free)(void *) = 0;
static void malloc_init(void) {
real_malloc = (void *(*)(unsigned long))dlsym(RTLD_NEXT, "malloc");
if (real_malloc == 0)
fprintf(stderr, "Error in `dlsym`: %s\n", dlerror());
}
static void free_init(void) {
real_free = (void (*)(void *))dlsym(RTLD_NEXT, "free");
if (real_free == 0)
fprintf(stderr, "Error in `dlsym`: %s\n", dlerror());
}
void *malloc(unsigned long size) {
if (real_malloc == 0)
malloc_init();
void *p = 0;
p = real_malloc(size);
malloc_counter++;
return p;
}
void free(void *p) {
if (real_free == 0)
free_init();
real_free(p);
malloc_counter--;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment