0
0
Power-electronicsHow-ToBeginner · 4 min read

How to Detect Memory Leaks in Embedded C: Simple Methods

To detect memory leaks in embedded C, use malloc and free tracking by logging allocations and deallocations, or use specialized tools like Valgrind or static analyzers. Monitoring memory usage over time and checking for mismatched malloc/free calls helps find leaks.
📐

Syntax

In embedded C, dynamic memory is managed using malloc to allocate and free to release memory. Tracking these calls helps detect leaks.

  • void* malloc(size_t size): Allocates size bytes and returns a pointer.
  • void free(void* ptr): Frees memory pointed by ptr.

To detect leaks, you can wrap these calls to log or count allocations and deallocations.

c
void* my_malloc(size_t size);
void my_free(void* ptr);
💻

Example

This example shows how to track memory allocations and frees by counting them. If the counts don't match, a leak exists.

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

static int alloc_count = 0;
static int free_count = 0;

void* my_malloc(size_t size) {
    void* ptr = malloc(size);
    if (ptr != NULL) {
        alloc_count++;
    }
    return ptr;
}

void my_free(void* ptr) {
    if (ptr != NULL) {
        free_count++;
        free(ptr);
    }
}

int main() {
    int* data1 = (int*)my_malloc(sizeof(int) * 10);
    int* data2 = (int*)my_malloc(sizeof(int) * 5);

    my_free(data1);
    // Intentionally forget to free data2 to simulate leak

    printf("Allocations: %d\n", alloc_count);
    printf("Frees: %d\n", free_count);

    if (alloc_count != free_count) {
        printf("Memory leak detected!\n");
    } else {
        printf("No memory leaks.\n");
    }

    return 0;
}
Output
Allocations: 2 Frees: 1 Memory leak detected!
⚠️

Common Pitfalls

Common mistakes when detecting memory leaks in embedded C include:

  • Not freeing all allocated memory, especially in error paths.
  • Double freeing the same pointer, causing undefined behavior.
  • Using malloc without checking if it returned NULL.
  • Relying only on runtime tests without static analysis or tools.

Always pair every malloc with a free and consider using tools or custom wrappers to track usage.

c
/* Wrong way: missing free */
int* ptr = malloc(sizeof(int) * 5);
// forgot free(ptr);

/* Right way: */
if (ptr != NULL) {
    // use ptr
    free(ptr);
}
📊

Quick Reference

Tips to detect memory leaks in embedded C:

  • Wrap malloc and free to count or log calls.
  • Use static analysis tools to find leaks before running code.
  • Monitor memory usage over time on the device.
  • Test all code paths, including errors, to ensure proper freeing.
  • Consider using tools like Valgrind on host simulations if possible.

Key Takeaways

Always pair every malloc with a free to avoid leaks.
Wrap allocation functions to track and log memory usage.
Use static analysis and runtime tools to detect leaks early.
Test all code paths, including error handling, for proper memory release.
Monitor memory usage over time on embedded devices to spot leaks.