Fix memory leak in TracyVector for non-trivially-destructible types #1191
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.
Summary
This PR fixes a memory leak in
TracyVectorwhere element destructors are not called for non-trivially-destructible types. The bug affects production code using nested containers likeVector<Vector<short_ptr<ZoneEvent>>>.Root Cause
TracyVectoris designed as a lightweight container optimized for POD types. However, it's also used with non-trivially-destructible types throughout Tracy's codebase. The current implementation never calls element destructors, causing memory leaks when elements manage resources (e.g., heap-allocated memory in nested vectors).Solution
Add compile-time type checking using C++17
if constexprwithstd::is_trivially_destructible_v<T>:This approach:
if constexpris evaluated at compile timeclear(),Realloc()Impact on Production Code
This bug affects critical data structures in
TracyWorker.hpp:TracyVector Usage Statistics:
TracyEvent.hpp(mostly POD types)TracyWorker.hpp(affected by this bug)Verification
Tested with AddressSanitizer/LeakSanitizer using Tracy's actual production types:
Before Fix
After Fix
Test Coverage
The fix includes a comprehensive test suite (
test_before_after_fix.cpp) with 6 test cases:Vector<Vector<short_ptr<ZoneEvent>>>)clear()with non-trivial typesVector<Vector<GhostZone>>)All tests pass with AddressSanitizer validation.
Backward Compatibility
Files Changed
server/TracyVector.hpp: Added<type_traits>include andif constexprchecks in 4 locationsRequest for Feedback
I'd appreciate feedback on:
Thank you for maintaining this excellent profiling tool!