Attempt to fix some lua userdata being corrupted when allocated via PoolAllocator

This commit is contained in:
Indev 2025-09-12 23:18:39 +03:00 committed by JugadorXEI
parent f4dd4a1be7
commit 811b2e5eee

View file

@ -14,6 +14,7 @@
#include "../cxxutil.hpp" #include "../cxxutil.hpp"
#include "../z_zone.h" #include "../z_zone.h"
#include "../lua_script.h"
using namespace srb2; using namespace srb2;
@ -117,6 +118,9 @@ void* PoolAllocator::allocate()
void PoolAllocator::deallocate(void* p) void PoolAllocator::deallocate(void* p)
{ {
// Required in case this block is reused
LUA_InvalidateUserdata(p);
FreeBlock* block = reinterpret_cast<FreeBlock*>(p); FreeBlock* block = reinterpret_cast<FreeBlock*>(p);
block->next = head_; block->next = head_;
head_ = block; head_ = block;
@ -127,6 +131,12 @@ void PoolAllocator::release()
ChunkFooter* next = nullptr; ChunkFooter* next = nullptr;
for (ChunkFooter* i = first_chunk_; i != nullptr; i = next) 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; next = i->next;
Z_Free(i->start); Z_Free(i->start);
} }