Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple memory leak detector for -O0 code or unit test mode #1809

Open
alexveden opened this issue Jan 11, 2025 · 6 comments
Open

Simple memory leak detector for -O0 code or unit test mode #1809

alexveden opened this issue Jan 11, 2025 · 6 comments
Labels
Additional info please Further information is requested

Comments

@alexveden
Copy link
Contributor

It would be great to have very simple memory leak check in allocators when doing debug builds. It simply tracks a number of allocs and number of frees via simple global variable counter.

In a warning message, c3c may advise compiling and run with --sanitize=address to find out exact place of memory leak.

I have similar in C:

const Allocator_i*
AllocatorGeneric_create(void)
{
#ifndef NDEBUG
    memset(&allocator__generic_data.stats, 0, sizeof(allocator__generic_data.stats));
#endif
    return &allocator__generic_data.base;
}

const Allocator_i*
AllocatorGeneric_destroy(void)
{
    allocator__generic_data.magic = 0;

#ifndef NDEBUG
    allocator_generic_s* a = &allocator__generic_data;
    // NOTE: this message only shown if no DNDEBUG
    if (a->stats.n_allocs != a->stats.n_free) {
        log$debug(
            "Allocator: Possible memory leaks/double free: memory allocator->allocs() [%u] != allocator->free() [%u] count! \n",
            a->stats.n_allocs,
            a->stats.n_free
        );
    }
    memset(&allocator__generic_data.stats, 0, sizeof(allocator__generic_data.stats));
#endif

    return NULL;
}

static void*
allocator_generic__malloc(usize size)
{
#ifndef NDEBUG
    allocator__generic_data.stats.n_allocs++;
#endif
    return malloc(size);
}

static void
allocator_generic__free(void* ptr)
{
#ifndef NDEBUG
    if (ptr != NULL) {
        allocator__generic_data.stats.n_free++;
    }
#endif
    free(ptr);
}
@lerno
Copy link
Collaborator

lerno commented Jan 11, 2025

What do you think about @assert_leaks?

@lerno
Copy link
Collaborator

lerno commented Jan 11, 2025

And of course the tracking heap allocator.

@lerno lerno added the Additional info please Further information is requested label Jan 11, 2025
@alexveden
Copy link
Contributor Author

And of course the tracking heap allocator.

That's cool. Also --sanitize=address track leaks with full stack print.

But these tools require special flags or developer efforts to enable them. I'm proposing to have lightweight and unintrusive rough checks for debug builds (or maybe on tests) enabled by default. Emitting just one warning message about possible leaks.

@lerno
Copy link
Collaborator

lerno commented Jan 12, 2025

Tracking allocations (necessary for leak detection) is a bit overkill for debug builds, however it could certainly be implemented (easily!) for tests by simply using the Tracking Allocator for running tests.

@alexveden
Copy link
Contributor Author

image
Do you mean maybe replacing it here, in test runner?

@lerno
Copy link
Collaborator

lerno commented Jan 13, 2025

The @pool stays, but the tracking allocator wraps the @pool

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Additional info please Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants