From fc48eb1592ce4d28990c6341e3d82d4719b2f80c Mon Sep 17 00:00:00 2001 From: Sidney Just Date: Thu, 12 Mar 2026 10:32:10 -0700 Subject: [PATCH 1/3] Added missing pool locks in the VmaDefragmenterContext constructor and destructor --- include/vk_mem_alloc.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/include/vk_mem_alloc.h b/include/vk_mem_alloc.h index 24792855..ae2c984f 100644 --- a/include/vk_mem_alloc.h +++ b/include/vk_mem_alloc.h @@ -12221,6 +12221,8 @@ VmaDefragmentationContext_T::VmaDefragmentationContext_T( m_BlockVectorCount = 1; m_PoolBlockVector = &info.pool->m_BlockVector; m_pBlockVectors = &m_PoolBlockVector; + + VmaMutexLockWrite lock(m_PoolBlockVector->m_Mutex, hAllocator->m_UseMutex); m_PoolBlockVector->SetIncrementalSort(false); m_PoolBlockVector->SortByFreeSize(); } @@ -12234,6 +12236,7 @@ VmaDefragmentationContext_T::VmaDefragmentationContext_T( VmaBlockVector* vector = m_pBlockVectors[i]; if (vector != VMA_NULL) { + VmaMutexLockWrite lock(vector->m_Mutex, hAllocator->m_UseMutex); vector->SetIncrementalSort(false); vector->SortByFreeSize(); } @@ -12264,6 +12267,7 @@ VmaDefragmentationContext_T::~VmaDefragmentationContext_T() { if (m_PoolBlockVector != VMA_NULL) { + VmaMutexLockWrite lock(m_PoolBlockVector->m_Mutex, m_PoolBlockVector->m_hAllocator->m_UseMutex); m_PoolBlockVector->SetIncrementalSort(true); } else @@ -12272,7 +12276,10 @@ VmaDefragmentationContext_T::~VmaDefragmentationContext_T() { VmaBlockVector* vector = m_pBlockVectors[i]; if (vector != VMA_NULL) + { + VmaMutexLockWrite lock(vector->m_Mutex, vector->m_hAllocator->m_UseMutex); vector->SetIncrementalSort(true); + } } } From cc26b9f978b1b5a1173edf752b9ab0da67e5f30e Mon Sep 17 00:00:00 2001 From: sawickiap Date: Fri, 13 Mar 2026 12:22:12 +0100 Subject: [PATCH 2/3] Updated CHANGELOG.md --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 26f4e9b6..694ed0fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,8 @@ - Added function `vmaGetMemoryWin32Handle2` offering extra parameter `VkExternalMemoryHandleTypeFlagBits handleType`. - Added `VMA_VERSION` macro with library version number (#507). - Improvements in the algorithm choosing memory type when `VMA_MEMORY_USAGE_AUTO*` is used (#520). -- Fixes for compatibility with C++20 modules on Clang 21 and GCC15 (#513, #514). +- Fixed compatibility with C++20 modules on Clang 21 and GCC15 (#513, #514). +- Fixed race condition in defragmentation (#529, #313). - Other fixes and improvements, including compatibility with various platforms and compilers, improvements in documentation, sample application, and tests. # 3.3.0 (2025-05-12) From c3c7a2c073c3bb721f23767334984c2da65cfba3 Mon Sep 17 00:00:00 2001 From: sawickiap Date: Sat, 16 May 2026 15:52:20 +0200 Subject: [PATCH 3/3] Added missing mutex lock in VmaDefragmentationContext_T::DefragmentPassEnd As proposed by @JustSid in https://github.com/GPUOpen-LibrariesAndSDKs/VulkanMemoryAllocator/issues/313 --- include/vk_mem_alloc.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/include/vk_mem_alloc.h b/include/vk_mem_alloc.h index ae2c984f..3934de34 100644 --- a/include/vk_mem_alloc.h +++ b/include/vk_mem_alloc.h @@ -12377,7 +12377,11 @@ VkResult VmaDefragmentationContext_T::DefragmentPassEnd(VmaDefragmentationPassMo { case VMA_DEFRAGMENTATION_MOVE_OPERATION_COPY: { - uint8_t mapCount = move.srcAllocation->SwapBlockAllocation(vector->m_hAllocator, move.dstTmpAllocation); + uint8_t mapCount = 0; + { + VmaMutexLockWrite swapLock(vector->GetMutex(), vector->GetAllocator()->m_UseMutex); + mapCount = move.srcAllocation->SwapBlockAllocation(vector->m_hAllocator, move.dstTmpAllocation); + } if (mapCount > 0) { allocator = vector->m_hAllocator;