From 9afcdb4623bc6ed7cd4225ea2c145078080cb98f 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 d10ff7eaf..9e591e78c 100644 --- a/src/core/memory.cpp +++ b/src/core/memory.cpp @@ -15,6 +15,7 @@ #include "../cxxutil.hpp" #include "../z_zone.h" +#include "../lua_script.h" using namespace srb2; @@ -118,6 +119,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 +132,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); }