Fix memory leak in VoxelBuffer::fill()
parent
b1b7d4ba03
commit
7161942ee1
|
@ -68,7 +68,12 @@ void register_voxel_types() {
|
|||
|
||||
void unregister_voxel_types() {
|
||||
|
||||
unsigned int used_blocks = VoxelMemoryPool::get_singleton()->debug_get_used_blocks();
|
||||
if (used_blocks > 0) {
|
||||
ERR_PRINT(String("VoxelMemoryPool: {0} memory blocks are still used when unregistering the module. Recycling leak?").format(varray(used_blocks)));
|
||||
}
|
||||
VoxelMemoryPool::destroy_singleton();
|
||||
|
||||
VoxelStringNames::destroy_singleton();
|
||||
|
||||
#ifdef TOOLS_ENABLED
|
||||
|
|
|
@ -128,8 +128,6 @@ void VoxelBuffer::fill(int defval, unsigned int channel_index) {
|
|||
channel.defval = defval;
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
create_channel_noinit(channel_index, _size);
|
||||
}
|
||||
|
||||
unsigned int volume = get_volume();
|
||||
|
@ -326,6 +324,7 @@ void VoxelBuffer::create_channel(int i, Vector3i size, uint8_t defval) {
|
|||
void VoxelBuffer::create_channel_noinit(int i, Vector3i size) {
|
||||
Channel &channel = _channels[i];
|
||||
unsigned int volume = size.x * size.y * size.z;
|
||||
CRASH_COND(channel.data != nullptr);
|
||||
#ifdef VOXEL_BUFFER_USE_MEMORY_POOL
|
||||
channel.data = VoxelMemoryPool::get_singleton()->allocate(volume);
|
||||
#else
|
||||
|
|
|
@ -28,7 +28,9 @@ VoxelMemoryPool::VoxelMemoryPool() {
|
|||
}
|
||||
|
||||
VoxelMemoryPool::~VoxelMemoryPool() {
|
||||
#ifdef TOOLS_ENABLED
|
||||
debug_print();
|
||||
#endif
|
||||
clear();
|
||||
memdelete(_mutex);
|
||||
}
|
||||
|
@ -43,6 +45,7 @@ uint8_t *VoxelMemoryPool::allocate(uint32_t size) {
|
|||
} else {
|
||||
block = (uint8_t *)memalloc(size * sizeof(uint8_t));
|
||||
}
|
||||
++_used_blocks;
|
||||
return block;
|
||||
}
|
||||
|
||||
|
@ -52,6 +55,7 @@ void VoxelMemoryPool::recycle(uint8_t *block, uint32_t size) {
|
|||
// Check recycling before having allocated
|
||||
CRASH_COND(pool == nullptr);
|
||||
pool->blocks.push_back(block);
|
||||
--_used_blocks;
|
||||
}
|
||||
|
||||
void VoxelMemoryPool::clear() {
|
||||
|
@ -81,6 +85,11 @@ void VoxelMemoryPool::debug_print() {
|
|||
}
|
||||
}
|
||||
|
||||
unsigned int VoxelMemoryPool::debug_get_used_blocks() const {
|
||||
MutexLock lock(_mutex);
|
||||
return _used_blocks;
|
||||
}
|
||||
|
||||
VoxelMemoryPool::Pool *VoxelMemoryPool::get_or_create_pool(uint32_t size) {
|
||||
Pool *pool;
|
||||
Pool **ppool = _pools.getptr(size);
|
||||
|
|
|
@ -24,14 +24,16 @@ public:
|
|||
|
||||
uint8_t *allocate(uint32_t size);
|
||||
void recycle(uint8_t *block, uint32_t size);
|
||||
void clear();
|
||||
|
||||
void debug_print();
|
||||
unsigned int debug_get_used_blocks() const;
|
||||
|
||||
private:
|
||||
Pool *get_or_create_pool(uint32_t size);
|
||||
void clear();
|
||||
|
||||
HashMap<uint32_t, Pool *> _pools;
|
||||
unsigned int _used_blocks = 0;
|
||||
Mutex *_mutex = nullptr;
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue