Add comprehensive memory leak testing #19
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Make sure to read the contributing guidelines before submitting a PR
Summary
Adds comprehensive memory leak regression testing to expand beyond the current AddressSanitizer-only approach. This PR introduces a dedicated test suite (
test-memory-leaks.cpp
) with 10 focused tests covering various memory lifecycle scenarios, plus optional Valgrind integration.Link to Devin run: https://app.devin.ai/sessions/9c9b287f31684d78a93467d4f27da6fc
Requested by: @stephencornwell
Changes
New Test File:
tests/test-memory-leaks.cpp
A dedicated memory leak regression test with 10 test functions:
llama_get_memory()
+llama_memory_clear()
Build System Changes:
tests/CMakeLists.txt
llama_build_and_test()
macro (LABEL "model")make test-valgrind
target when Valgrind is installedTesting Approach
The test follows existing patterns from
test-autorelease.cpp
andtest-thread-safety.cpp
:get-model.cpp
helper for model paths (skips gracefully if no model provided)LLAMA_SANITIZE_ADDRESS=ON
Run with:
Review Checklist
Memory API correctness: Verify
llama_get_memory(ctx)
→llama_memory_clear(mem, false)
is the correct pattern for KV cache clearing (test_kv_cache_clear_operations)Threading safety: Review
test_threaded_contexts
- creates 3 threads sharing one model with separate contexts. Ensure this follows thread-safety best practices for the library.Cleanup order: Confirm sampler → context → model → backend order is correct in all scenarios, especially error paths.
Valgrind CMake syntax: Verify the custom target configuration is correct (untested locally as Valgrind wasn't installed).
CI validation: This will be the first run with actual models and sanitizers - local testing was limited by environment issues and no model file.
Notes
test-opt.cpp remains disabled: While investigating, identified the root cause of its memory leak (line 300:
ggml_opt_alloc()
called in loop withoutggml_opt_reset()
between iterations), but didn't fix it per task scope. This could be addressed in a follow-up if desired.Local testing limitations: Successfully compiled with AddressSanitizer and test ran via CTest (skipped due to no model), but couldn't validate full test suite locally due to ASan runtime environment issues. CI will provide the first comprehensive validation.
Test philosophy: These tests are designed as regression tests - they pass if no leaks are detected by sanitizers/Valgrind, rather than asserting on memory usage numbers directly.