From d72a26a0732724de8eef728242ea94b81db3b45d Mon Sep 17 00:00:00 2001 From: Indev Date: Fri, 12 Sep 2025 23:18:39 +0300 Subject: [PATCH] Attempt to fix some lua userdata being corrupted when allocated via PoolAllocator --- src/core/memory.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/core/memory.cpp b/src/core/memory.cpp index 219f64bba..02249463f 100644 --- a/src/core/memory.cpp +++ b/src/core/memory.cpp @@ -16,6 +16,8 @@ #include "../z_zone.h" #include +#include "../lua_script.h" + using namespace srb2; namespace @@ -118,6 +120,9 @@ void* PoolAllocator::allocate() void PoolAllocator::deallocate(void* p) { + // Required in case this block is reused + LUA_InvalidateUserdata(p); + FreeBlock* block = reinterpret_cast(p); block->next = head_; head_ = block; @@ -128,6 +133,12 @@ void PoolAllocator::release() ChunkFooter* next = nullptr; for (ChunkFooter* i = first_chunk_; i != nullptr; i = next) { + uint8_t *chunk = (uint8_t*)i->start; + for (size_t j = 0; j < blocks_; j++) + { + // Invalidate all blocks that possibly weren't passed to deallocate + LUA_InvalidateUserdata(chunk + (j * block_size_)); + } next = i->next; Z_Free(i->start); }