From 8d5b3d16e4d3fc931cfd5b34a490461b8953cb1d Mon Sep 17 00:00:00 2001 From: David Reid Date: Wed, 18 Aug 2021 20:51:34 +1000 Subject: [PATCH] Add some double-free detection to the slot allocator. --- miniaudio.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/miniaudio.h b/miniaudio.h index 6820819b..6f33c0f7 100644 --- a/miniaudio.h +++ b/miniaudio.h @@ -38994,6 +38994,15 @@ MA_API ma_result ma_slot_allocator_free(ma_slot_allocator* pAllocator, ma_uint64 oldBitfield = c89atomic_load_32(&pAllocator->pGroups[iGroup].bitfield); /* <-- This copy must happen. The compiler must not optimize this away. */ newBitfield = oldBitfield & ~(1 << iBit); + /* Debugging for checking for double-frees. */ + #if defined(MA_DEBUG_OUTPUT) + { + if ((oldBitfield & (1 << iBit)) == 0) { + MA_ASSERT(MA_FALSE); /* Double free detected.*/ + } + } + #endif + if (c89atomic_compare_and_swap_32(&pAllocator->pGroups[iGroup].bitfield, oldBitfield, newBitfield) == oldBitfield) { c89atomic_fetch_sub_32(&pAllocator->count, 1); return MA_SUCCESS;