From 811b2e5eee80c984d608b6e784a8013d45343fdf 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 | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/core/memory.cpp b/src/core/memory.cpp index b6e0387e9..1841584ee 100644 --- a/src/core/memory.cpp +++ b/src/core/memory.cpp @@ -14,6 +14,7 @@ #include "../cxxutil.hpp" #include "../z_zone.h" +#include "../lua_script.h" using namespace srb2; @@ -117,6 +118,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; @@ -127,6 +131,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); }