From 54cd8f8786bd47dbbd093cc79cb95df6cb520969 Mon Sep 17 00:00:00 2001
From: Clark Coleman <clc@zephyr-software.com>
Date: Sat, 23 Feb 2019 12:34:13 -0500
Subject: [PATCH] Fix some memory leaks.

---
 include/interfaces/idapro/STARSInterface.h |  6 +++++
 src/base/SMPFunction.cpp                   | 30 ++++++++++++++++++++++
 src/base/SMPInstr.cpp                      |  2 +-
 3 files changed, 37 insertions(+), 1 deletion(-)

diff --git a/include/interfaces/idapro/STARSInterface.h b/include/interfaces/idapro/STARSInterface.h
index a8d8df2d..80000d24 100644
--- a/include/interfaces/idapro/STARSInterface.h
+++ b/include/interfaces/idapro/STARSInterface.h
@@ -24,6 +24,12 @@ public:
 	// Constructors
 	STARS_IDA_Interface_t() : CGCBinary(false) {};
 
+	// Destructor
+	~STARS_IDA_Interface_t() {
+		this->funcmap.clear();
+		this->segmap.clear();
+	}
+
 	// Segment accessors
 
 	virtual STARS_Segment_t *getseg(const STARS_ea_t &addr) {
diff --git a/src/base/SMPFunction.cpp b/src/base/SMPFunction.cpp
index e7b0506a..53a8e59b 100644
--- a/src/base/SMPFunction.cpp
+++ b/src/base/SMPFunction.cpp
@@ -391,6 +391,36 @@ SMPFunction::~SMPFunction() {
 		//  when we saved the exprs for SPARK Ada translation.
 		this->DestroyLoopExprs();
 	}
+
+	for (size_t i = 0; i < this->ShadowCFGBlocks.size(); ++i) {
+		if (nullptr != this->ShadowCFGBlocks[i])
+			delete this->ShadowCFGBlocks[i];
+	}
+
+	for (size_t i = 0; i < this->LoopInductionVars.size(); ++i) {
+		for (struct InductionVarFamily CurrFam : this->LoopInductionVars[i]) {
+			if (nullptr != CurrFam.BIVInitExpr)
+				delete CurrFam.BIVInitExpr;
+			if (nullptr != CurrFam.BIVLimitExpr)
+				delete CurrFam.BIVLimitExpr;
+		}
+	}
+
+	for (size_t i = 0; i < this->LoopMemAddrExprsFromCalleeLoops.size(); ++i) {
+		for (STARSExpression *CurrExpr : this->LoopMemAddrExprsFromCalleeLoops[i]) {
+			if (nullptr != CurrExpr)
+				delete CurrExpr;
+		}
+	}
+
+	for (size_t i = 0; i < this->LoopMemAddrExprsFromCallees.size(); ++i) {
+		for (STARSExpression *CurrExpr : this->LoopMemAddrExprsFromCallees[i]) {
+			if (nullptr != CurrExpr)
+				delete CurrExpr;
+		}
+	}
+
+	return;
 } // end of SMPFunction::destructor
 
 void SMPFunction::DestroyLoopExprs(void) {
diff --git a/src/base/SMPInstr.cpp b/src/base/SMPInstr.cpp
index af1b6e2b..61fb134a 100644
--- a/src/base/SMPInstr.cpp
+++ b/src/base/SMPInstr.cpp
@@ -4931,7 +4931,7 @@ SMPInstr::~SMPInstr() {
 		this->Defs.clear();
 		this->Uses.clear();
 	}
-#if 0  // fix crashes before enabling this
+#if 1  // fix crashes before enabling this
 	if (nullptr != this->STARSInstPtr) {
 		delete this->STARSInstPtr;
 		this->STARSInstPtr = nullptr;
-- 
GitLab