From fce58131916103f3c2120adafcf59895a402a634 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Sat, 30 Jun 2012 21:15:34 +0300 Subject: Extend leak detection features. --- src/Makefile.am | 2 +- src/commandhandler.cpp | 4 ++-- src/localconsts.h | 1 + src/main.h | 1 - src/playerinfo.cpp | 1 + src/resources/resourcemanager.cpp | 40 ++++++++++++++++++++++++++++++++++----- src/resources/resourcemanager.h | 2 +- 7 files changed, 41 insertions(+), 10 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index b4010db58..8eac3d495 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -11,7 +11,7 @@ manaplus_CXXFLAGS += -DENABLE_PORTABLE endif if ENABLE_MEM_DEBUG -manaplus_CXXFLAGS += -DENABLE_MEM_DEBUG +manaplus_CXXFLAGS += -DENABLE_MEM_DEBUG -DDEBUG_DUMP_LEAKS manaplus_SOURCES = debug/debug_new.cpp \ debug/debug_new.h \ diff --git a/src/commandhandler.cpp b/src/commandhandler.cpp index 1afc332cb..8ed68ae20 100644 --- a/src/commandhandler.cpp +++ b/src/commandhandler.cpp @@ -61,7 +61,7 @@ #include "openglgraphics.h" #endif -#ifdef DEBUG_DUMP_LEAKS +#ifdef DEBUG_DUMP_LEAKS1 #include "resources/image.h" #include "resources/resource.h" #include "resources/resourcemanager.h" @@ -1212,7 +1212,7 @@ void CommandHandler::handleError(const std::string &args A_UNUSED, logger->log("test %d", *ptr); } -#ifdef DEBUG_DUMP_LEAKS +#ifdef DEBUG_DUMP_LEAKS1 void showRes(std::string str, ResourceManager::Resources *res); void showRes(std::string str, ResourceManager::Resources *res) diff --git a/src/localconsts.h b/src/localconsts.h index 9f2454ba2..b01718d92 100644 --- a/src/localconsts.h +++ b/src/localconsts.h @@ -35,3 +35,4 @@ //#define DEBUG_CONFIG 1 //#define DEBUG_BIND_TEXTURE 1 //#define DISABLE_RESOURCE_CACHING 1 +#define DUMP_LEAKED_RESOURCES 1 diff --git a/src/main.h b/src/main.h index 583079875..f3feec1ff 100644 --- a/src/main.h +++ b/src/main.h @@ -81,7 +81,6 @@ #define ENABLEDEBUGLOG 1 //define DEBUG_LEAKS 1 -//define DEBUG_DUMP_LEAKS 1 //define DEBUG_CONFIG 1 //define DEBUG_FONT 1 //define DEBUG_FONT_COUNTERS 1 diff --git a/src/playerinfo.cpp b/src/playerinfo.cpp index 2500e298e..eaf6e1350 100644 --- a/src/playerinfo.cpp +++ b/src/playerinfo.cpp @@ -357,6 +357,7 @@ void init() void deinit() { + clearInventory(); delete mListener; mListener = nullptr; } diff --git a/src/resources/resourcemanager.cpp b/src/resources/resourcemanager.cpp index 9b5394687..452bc9942 100644 --- a/src/resources/resourcemanager.cpp +++ b/src/resources/resourcemanager.cpp @@ -177,9 +177,12 @@ void ResourceManager::cleanUp(Resource *res) } delete res; +#ifdef DEBUG_LEAKS + cleanOrpahns(true); +#endif } -void ResourceManager::cleanOrphans(bool always) +bool ResourceManager::cleanOrphans(bool always) { timeval tv; gettimeofday(&tv, nullptr); @@ -187,8 +190,10 @@ void ResourceManager::cleanOrphans(bool always) time_t oldest = tv.tv_sec, threshold = oldest - 30; if (mOrphanedResources.empty() || (!always && mOldestOrphan >= threshold)) - return; + return false; + bool status(false); + status = false; ResourceIterator iter = mOrphanedResources.begin(); while (iter != mOrphanedResources.end()) { @@ -199,10 +204,11 @@ void ResourceManager::cleanOrphans(bool always) continue; } time_t t = res->mTimeStamp; - if (t >= threshold) + if (!always && t >= threshold) { - if (t < oldest) oldest = t; - ++iter; + if (t < oldest) + oldest = t; + ++ iter; } else { @@ -212,10 +218,12 @@ void ResourceManager::cleanOrphans(bool always) mOrphanedResources.erase(toErase); delete res; // delete only after removal from list, // to avoid issues in recursion + status = true; } } mOldestOrphan = oldest; + return status; } bool ResourceManager::setWriteDir(const std::string &path) @@ -602,6 +610,28 @@ ResourceManager *ResourceManager::getInstance() void ResourceManager::deleteInstance() { +#ifdef DUMP_LEAKED_RESOURCES + if (instance) + { + logger->log("clean orphans start"); + while(instance->cleanOrphans(true)); + logger->log("clean orphans end"); + ResourceIterator iter = instance->mResources.begin(); + + while (iter != instance->mResources.end()) + { + if (iter->second) + { + if (iter->second->getRefCount()) + { + logger->log("ResourceLeak: " + iter->second->getIdPath() + + " (" + toString(iter->second->getRefCount()) + ")"); + } + } + ++iter; + } + } +#endif delete instance; instance = nullptr; } diff --git a/src/resources/resourcemanager.h b/src/resources/resourcemanager.h index 0eadf683d..b07b23431 100644 --- a/src/resources/resourcemanager.h +++ b/src/resources/resourcemanager.h @@ -276,7 +276,7 @@ class ResourceManager { return &mOrphanedResources; } #endif - void cleanOrphans(bool always = false); + bool cleanOrphans(bool always = false); static void addDelayedAnimation(AnimationDelayLoad *animation) { mDelayedAnimations.push_back(animation); } -- cgit v1.2.3-60-g2f50