From e09fe6ff8a5458161c0cea8e184fead84d8f7185 Mon Sep 17 00:00:00 2001 From: Benau Date: Thu, 13 Oct 2016 14:08:58 +0800 Subject: [PATCH] Fix memory leak of mesh tool --- src/graphics/mesh_tools.cpp | 38 ++++++++++++++++++------------------- src/tracks/track.cpp | 2 +- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/src/graphics/mesh_tools.cpp b/src/graphics/mesh_tools.cpp index f9698acc6..b3792b1d5 100644 --- a/src/graphics/mesh_tools.cpp +++ b/src/graphics/mesh_tools.cpp @@ -444,29 +444,27 @@ scene::IMesh* MeshTools::createMeshWithTangents(scene::IMesh* mesh, clone->recalculateBoundingBox(); if (calculate_tangents) recalculateTangents(clone, recalculate_normals, smooth, angle_weighted); - - int mbcount = clone->getMeshBufferCount(); - for (int i = 0; i < mbcount; i++) - { - scene::IMeshBuffer* mb = clone->getMeshBuffer(i); - - for (u32 t = 0; t < video::MATERIAL_MAX_TEXTURES; t++) - { - video::ITexture* texture = mb->getMaterial().TextureLayer[t].Texture; - if (texture != NULL) - texture->grab(); - } - } scene::IMeshCache* meshCache = irr_driver->getSceneManager()->getMeshCache(); io::SNamedPath path = meshCache->getMeshName(mesh); - irr_driver->removeMeshFromCache(mesh); + if (path.getPath() == "") + { + // This mesh is not in irrlicht cache, drop it directly + assert(mesh->getReferenceCount() == 1); + mesh->drop(); + return clone; + } + else + { + // Cache the calcuated tangent mesh with path + irr_driver->removeMeshFromCache(mesh); + scene::SAnimatedMesh* amesh = new scene::SAnimatedMesh(clone); + clone->drop(); + irr_driver->grabAllTextures(amesh); + meshCache->addMesh(path, amesh); + World::getWorld()->getTrack()->addCachedMesh(amesh); - scene::SAnimatedMesh* amesh = new scene::SAnimatedMesh(clone); - meshCache->addMesh(path, amesh); + return amesh; + } - World::getWorld()->getTrack()->addCachedMesh(amesh); - - return clone; } - diff --git a/src/tracks/track.cpp b/src/tracks/track.cpp index e01eaffed..39ce3d986 100644 --- a/src/tracks/track.cpp +++ b/src/tracks/track.cpp @@ -1174,7 +1174,7 @@ bool Track::loadMainTrack(const XMLNode &root) handleAnimatedTextures(scene_node, *track_node); m_all_nodes.push_back(scene_node); - MeshTools::minMax3D(merged_mesh, &m_aabb_min, &m_aabb_max); + MeshTools::minMax3D(tangent_mesh, &m_aabb_min, &m_aabb_max); // Increase the maximum height of the track: since items that fly // too high explode, e.g. cakes can not be show when being at the // top of the track (since they will explode when leaving the AABB