Repository navigation
Conversation
A margin block was recognized only by its size, so a free block of exactly VMA_DEBUG_MARGIN bytes (left by alignment, or by a split) was taken for a margin and never merged with a freed neighbor. After every allocation of the block was freed, the null block did not return to offset 0: IsEmpty() stayed false, the block was never released, and destroying the pool or the allocator hit the "Some allocations were not freed before destruction of this memory block!" assert with nothing leaked. A margin block is now recognized by what precedes it: it always follows the taken block it belongs to, and no other free block follows a taken block. Nothing changes when VMA_DEBUG_MARGIN is 0.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
With
VMA_DEBUG_MARGIN> 0, a block managed by the TLSF algorithm can stay "not empty" after every allocation in it was freed. The block is then never released, and destroying the pool or the allocator hitsSome allocations were not freed before destruction of this memory block!although nothing leaked.How to reproduce
VMA_DEBUG_MARGIN= 16, a custom pool with one block, two allocations made withvmaAllocateMemory:vmaCalculatePoolStatisticsthen reports 0 allocations and 2 unused ranges (288 bytes and the rest) instead of 1, andvmaDestroyPoolasserts. Freeing in the other order, or with a gap of another size (alignment 64 or 16), is fine. With random sizes and alignments it happens sooner or later in every run I tried, because any free block of exactlyVMA_DEBUG_MARGINbytes triggers it, whether it comes from alignment or from a split.Cause
A margin is kept as a free block of
VMA_DEBUG_MARGINbytes after each allocation, andVmaBlockMetadata_TLSF::Allocand::Freetell a margin from an ordinary free block by its size alone (prev->size != debugMargin). A real free block that happens to have that size is treated as a margin and is not merged with the freed block next to it. In the sequence above, freeing the second allocation skips the 16-byte gap and merges only into the null block; freeing the first then merges forward into the gap, but nothing merges the result with the null block, whose offset never returns to 0.Fix
A margin block is recognized by what precedes it: it always follows the taken block it belongs to, and no other free block follows a taken block, since every taken block is followed by its margin. The two size checks now call a small helper,
IsMarginBlock. Nothing changes whenVMA_DEBUG_MARGINis 0.Testing
I tested with a standalone program (below) on Linux, with
VMA_HEAVY_ASSERTmapped toVMA_ASSERTso thatValidate()runs after every allocation and free:VMA_DEBUG_MARGIN= 16 (with and withoutVMA_DEBUG_DETECT_CORRUPTION) and withVMA_DEBUG_MARGIN= 0.I did not run
src/Tests.cpp: the sample is Windows-only and I tested on Linux. I can add a case toTestDebugMarginfor this if you would like one.Standalone reproduction (the two-allocation case)