From a8a8a03a853e10754d6b6a008cd0ddac482cef0c Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Fri, 19 Nov 2004 17:09:48 +0000 Subject: [PATCH] Fix memory leaks, patch contributed by Morten Ofstad! llvm-svn: 17999 --- llvm/lib/VMCore/LeakDetector.cpp | 36 +++++++++++++++++++------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/llvm/lib/VMCore/LeakDetector.cpp b/llvm/lib/VMCore/LeakDetector.cpp index 807bd22b0755..c8e7f4c6a52a 100644 --- a/llvm/lib/VMCore/LeakDetector.cpp +++ b/llvm/lib/VMCore/LeakDetector.cpp @@ -68,9 +68,6 @@ namespace { } std::cerr << '\n'; - // Clear out results so we don't get duplicate warnings on - // next call... - Ts.clear(); return true; } return false; @@ -82,21 +79,26 @@ namespace { const char* const Name; }; - typedef LeakDetectorImpl Objects; - typedef LeakDetectorImpl LLVMObjects; + LeakDetectorImpl *Objects; + LeakDetectorImpl *LLVMObjects; - Objects& getObjects() { - static Objects *o = 0; - if (o == 0) - o = new Objects("GENERIC"); - return *o; + LeakDetectorImpl &getObjects() { + if (Objects == 0) + Objects = new LeakDetectorImpl("GENERIC"); + return *Objects; } - LLVMObjects& getLLVMObjects() { - static LLVMObjects *o = 0; - if (o == 0) - o = new LLVMObjects("LLVM"); - return *o; + LeakDetectorImpl &getLLVMObjects() { + if (LLVMObjects == 0) + LLVMObjects = new LeakDetectorImpl("LLVM"); + return *LLVMObjects; + } + + void clearGarbage() { + delete Objects; + delete LLVMObjects; + Objects = 0; + LLVMObjects = 0; } } @@ -122,4 +124,8 @@ void LeakDetector::checkForGarbageImpl(const std::string &Message) { getLLVMObjects().hasGarbage(Message)) std::cerr << "\nThis is probably because you removed an object, but didn't " "delete it. Please check your code for memory leaks.\n"; + + // Clear out results so we don't get duplicate warnings on + // next call... + clearGarbage(); }