From 63c7c0536c60f1ec43b6c3a2d2379dae15ae5e69 Mon Sep 17 00:00:00 2001 From: Relintai Date: Wed, 14 Aug 2019 00:29:28 +0200 Subject: [PATCH] Fixed a memleak. --- library/voxel_surface.cpp | 8 ++++++-- library/voxel_surface.h | 2 +- library/voxelman_library.cpp | 16 +++++++++++++--- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/library/voxel_surface.cpp b/library/voxel_surface.cpp index b8d5426..a6f1f4e 100644 --- a/library/voxel_surface.cpp +++ b/library/voxel_surface.cpp @@ -56,7 +56,7 @@ Ref VoxelSurface::get_library() const { } void VoxelSurface::set_library(Ref library) { - _library = library; + _library = (*library); } Vector2 VoxelSurface::transform_uv(const int side, const Vector2 uv) const { @@ -74,6 +74,8 @@ VoxelSurface::VoxelSurface() { } _is_transparent = false; + + _library = NULL; } VoxelSurface::VoxelSurface(int id) { @@ -84,10 +86,12 @@ VoxelSurface::VoxelSurface(int id) { } _is_transparent = false; + + _library = NULL; } VoxelSurface::~VoxelSurface() { - _library.unref(); + //_library = NULL; } void VoxelSurface::_bind_methods() { diff --git a/library/voxel_surface.h b/library/voxel_surface.h index 21d61e8..6513f0f 100644 --- a/library/voxel_surface.h +++ b/library/voxel_surface.h @@ -66,7 +66,7 @@ protected: static void _bind_methods(); private: - Ref _library; + VoxelmanLibrary *_library; int _id; String _name; diff --git a/library/voxelman_library.cpp b/library/voxelman_library.cpp index e92b247..ddc320b 100644 --- a/library/voxelman_library.cpp +++ b/library/voxelman_library.cpp @@ -12,13 +12,21 @@ VoxelmanLibrary::VoxelmanLibrary() { for (int i = 0; i < MAX_VOXEL_TYPES; ++i) { if (_voxel_types[i] != NULL) { - print_error("set_this"); _voxel_types[i]->set_library(Ref(this)); } } } VoxelmanLibrary::~VoxelmanLibrary() { + for (int i = 0; i < MAX_VOXEL_TYPES; ++i) { + if (_voxel_types[i].is_valid()) { + _voxel_types[i]->set_library(Ref(NULL)); + _voxel_types[i].unref(); + } + } + + _material.unref(); + _prop_material.unref(); } Ref VoxelmanLibrary::get_voxel_surface(int index) const { @@ -29,10 +37,12 @@ Ref VoxelmanLibrary::get_voxel_surface(int index) const { void VoxelmanLibrary::set_voxel_surface(int index, Ref value) { ERR_FAIL_COND(index < 0 || index > _voxel_types_count); - ERR_FAIL_COND(value == NULL); - if (value != NULL) { + if (_voxel_types[index].is_valid()) { + _voxel_types[index]->set_library(Ref(NULL)); + } + if (value.is_valid()) { value->set_library(Ref(this)); _voxel_types[index] = Ref(value);