]> granicus.if.org Git - clang/commitdiff
Fix a memory leak in the analyzer - BugReports didn't get freed. Plus, remove invalid...
authorAnna Zaks <ganna@apple.com>
Fri, 19 Aug 2011 01:57:09 +0000 (01:57 +0000)
committerAnna Zaks <ganna@apple.com>
Fri, 19 Aug 2011 01:57:09 +0000 (01:57 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@138027 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/StaticAnalyzer/Core/BugReporter/BugReporter.h
lib/StaticAnalyzer/Core/BugReporter.cpp

index 2b9d1a89f51c920abac512c39d8f01e1160c7db2..421e305508a7afb9efaa9001fa07137fa55d9ae5 100644 (file)
@@ -128,7 +128,7 @@ public:
 
   virtual ~BugReport();
 
-  bool isOwnedByReporterContext() { return false; }
+  virtual bool isOwnedByReporterContext() { return false; }
 
   const BugType& getBugType() const { return BT; }
   BugType& getBugType() { return BT; }
@@ -275,6 +275,8 @@ private:
 
   /// The set of bug reports tracked by the BugReporter.
   llvm::FoldingSet<BugReportEquivClass> EQClasses;
+  /// A vector of BugReports for tracking the allocated pointers and cleanup.
+  std::vector<BugReportEquivClass *> EQClassesVector;
 
 protected:
   BugReporter(BugReporterData& d, Kind k) : BugTypes(F.getEmptySet()), kind(k),
index 6624b93d2bf34e7f689a83e25e4acc82b5e98c64..6821806c8467715e4b8c1e921866f5a2395f13f9 100644 (file)
@@ -1224,9 +1224,9 @@ void BugReport::addVisitor(BugReporterVisitor* visitor) {
 
 BugReport::~BugReport() {
   for (visitor_iterator I = visitor_begin(), E = visitor_end(); I != E; ++I) {
-    // TODO: Remove the isOwned method; use reference counting to track visitors?.
-    assert((*I)->isOwnedByReporterContext());
-    delete *I;
+    if ((*I)->isOwnedByReporterContext()) {
+      delete *I;
+    }
   }
 }
 
@@ -1363,7 +1363,16 @@ ExplodedGraph &GRBugReporter::getGraph() { return Eng.getGraph(); }
 ProgramStateManager&
 GRBugReporter::getStateManager() { return Eng.getStateManager(); }
 
-BugReporter::~BugReporter() { FlushReports(); }
+BugReporter::~BugReporter() {
+  FlushReports();
+
+  // Free the bug reports we are tracking.
+  typedef std::vector<BugReportEquivClass *> ContTy;
+  for (ContTy::iterator I = EQClassesVector.begin(), E = EQClassesVector.end();
+       I != E; ++I) {
+    delete *I;
+  }
+}
 
 void BugReporter::FlushReports() {
   if (BugTypes.isEmpty())
@@ -1694,6 +1703,7 @@ void BugReporter::EmitReport(BugReport* R) {
   if (!EQ) {
     EQ = new BugReportEquivClass(R);
     EQClasses.InsertNode(EQ, InsertPos);
+    EQClassesVector.push_back(EQ);
   }
   else
     EQ->AddReport(R);