From db190f271a62a10e9ca653eaaca3ab4853d67ea0 Mon Sep 17 00:00:00 2001
From: Russell Taylor <taylorrj@ornl.gov>
Date: Thu, 25 Mar 2010 11:30:11 +0000
Subject: [PATCH] Fix various memory leaks (mostly only in tests) and other
 problems flagged up by valgrind. Re #1154.

---
 Code/Mantid/API/src/CompositeFunction.cpp     |  4 ++--
 Code/Mantid/API/src/ParameterTie.cpp          |  2 --
 Code/Mantid/API/test/AlgorithmHistoryTest.h   |  2 ++
 Code/Mantid/API/test/AxisTest.h               |  3 ++-
 Code/Mantid/API/test/CompositeFunctionTest.h  | 24 ++++++++++++-------
 Code/Mantid/API/test/FunctionFactoryTest.h    | 21 ++++++++++------
 Code/Mantid/API/test/LogParserTest.h          | 11 ++++++---
 Code/Mantid/API/test/MatrixWorkspaceTest.h    |  3 +++
 Code/Mantid/API/test/ParameterReferenceTest.h |  3 ++-
 Code/Mantid/API/test/runTests.sh              |  5 +---
 Code/Mantid/CurveFitting/src/Fit.cpp          | 20 +++++++++-------
 Code/Mantid/CurveFitting/src/Fit1D.cpp        |  8 ++++---
 .../Mantid/CurveFitting/test/ResolutionTest.h |  3 +--
 Code/Mantid/CurveFitting/test/runTests.sh     |  6 ++---
 .../inc/MantidDataHandling/SaveSPE.h          | 24 +++++++++----------
 Code/Mantid/DataHandling/src/SaveSPE.cpp      |  2 ++
 Code/Mantid/DataHandling/test/runTests.sh     |  8 ++-----
 .../Mantid/DataObjects/src/TableWorkspace.cpp |  6 +++--
 .../test/CompressedWorkspace2DTest.h          |  1 +
 .../DataObjects/test/ManagedWorkspace2DTest.h |  1 +
 Code/Mantid/DataObjects/test/RefAxisTest.h    |  9 ++++---
 .../test/WorkspaceValidatorsTest.h            |  1 +
 Code/Mantid/DataObjects/test/runTests.sh      | 10 ++++----
 Code/Mantid/Geometry/test/runTests.sh         |  5 +---
 Code/Mantid/Kernel/test/FilePropertyTest.h    |  3 +++
 Code/Mantid/Kernel/test/LogFilterTest.h       |  7 +++++-
 Code/Mantid/Kernel/test/PropertyTest.h        |  5 ++++
 .../Kernel/test/TimeSeriesPropertyTest.h      |  7 ++++++
 Code/Mantid/Kernel/test/runTests.sh           |  3 +--
 Code/Mantid/Nexus/src/LoadNexusProcessed.cpp  |  1 +
 Code/Mantid/Nexus/src/NexusClasses.cpp        |  2 +-
 Code/Mantid/Nexus/test/runTests.sh            |  6 ++---
 Code/Mantid/PythonAPI/test/runTests.sh        |  8 ++-----
 33 files changed, 132 insertions(+), 92 deletions(-)

diff --git a/Code/Mantid/API/src/CompositeFunction.cpp b/Code/Mantid/API/src/CompositeFunction.cpp
index 4ab567a2080..143751d2d68 100644
--- a/Code/Mantid/API/src/CompositeFunction.cpp
+++ b/Code/Mantid/API/src/CompositeFunction.cpp
@@ -256,7 +256,7 @@ int CompositeFunction::parameterIndex(const std::string& name)const
   if (index < 0)
     throw std::invalid_argument("CompositeFunction::getParameter: parameter name must contain function index");
 
-  return m_paramOffsets[index] + getFunction(index)->parameterIndex(pname);
+  return getFunction(index)->parameterIndex(pname) + m_paramOffsets[index];
 }
 
 /**
@@ -408,7 +408,7 @@ void CompositeFunction::checkFunction()
   std::vector<IFunction*> functions(m_functions.begin(),m_functions.end());
   m_functions.clear();
 
-  for(int i=0;i<functions.size();i++)
+  for(std::vector<IFunction*>::size_type i=0;i<functions.size();i++)
   {
     IFunction* f = functions[i];
     CompositeFunction* cf = dynamic_cast<CompositeFunction*>(f);
diff --git a/Code/Mantid/API/src/ParameterTie.cpp b/Code/Mantid/API/src/ParameterTie.cpp
index d2c8e4892f6..d6f03a223d2 100644
--- a/Code/Mantid/API/src/ParameterTie.cpp
+++ b/Code/Mantid/API/src/ParameterTie.cpp
@@ -126,8 +126,6 @@ namespace API
     {
       fun = getFunction();
     }
-    const CompositeFunction* cf  = dynamic_cast<const CompositeFunction*>(fun);
-    CompositeFunction* cf1 = dynamic_cast<CompositeFunction*>(m_function1);
     std::string res_expression;
     try
     {
diff --git a/Code/Mantid/API/test/AlgorithmHistoryTest.h b/Code/Mantid/API/test/AlgorithmHistoryTest.h
index 9919773523c..e1ec931d12d 100644
--- a/Code/Mantid/API/test/AlgorithmHistoryTest.h
+++ b/Code/Mantid/API/test/AlgorithmHistoryTest.h
@@ -75,6 +75,8 @@ public:
     TS_ASSERT_THROWS_NOTHING(output << AH);
     TS_ASSERT_EQUALS(output.str(),correctOutput);
 
+    delete timeinfo;
+    delete alg;
   }
 
 
diff --git a/Code/Mantid/API/test/AxisTest.h b/Code/Mantid/API/test/AxisTest.h
index f2bdbd8cd39..0f408754560 100644
--- a/Code/Mantid/API/test/AxisTest.h
+++ b/Code/Mantid/API/test/AxisTest.h
@@ -68,7 +68,8 @@ public:
     TS_ASSERT_DIFFERS( newSpecAxis, spectraAxis )
     Axis* newNumAxis = numericAxis->clone();
     TS_ASSERT_DIFFERS( newNumAxis, numericAxis )
-    delete newSpecAxis, newNumAxis;
+    delete newSpecAxis;
+    delete newNumAxis;
   }
   
   void testTitle()
diff --git a/Code/Mantid/API/test/CompositeFunctionTest.h b/Code/Mantid/API/test/CompositeFunctionTest.h
index 1239a49988f..7e9aa8d0eb3 100644
--- a/Code/Mantid/API/test/CompositeFunctionTest.h
+++ b/Code/Mantid/API/test/CompositeFunctionTest.h
@@ -401,7 +401,7 @@ public:
     TS_ASSERT_EQUALS(mfun->parameterIndex("f3.h"),10);
     TS_ASSERT_EQUALS(mfun->parameterIndex("f3.s"),11);
 
-
+    delete mfun;
   }
 
   void testSetActive()
@@ -466,7 +466,9 @@ public:
     TS_ASSERT_EQUALS(mfun->getParameter(8),103);
     TS_ASSERT_EQUALS(mfun->getParameter(9),104);
     TS_ASSERT_EQUALS(mfun->getParameter(10),3.2);
-    TS_ASSERT_EQUALS(mfun->getParameter(11),105);
+    TS_ASSERT_EQUALS(mfun->getParameter(11),105);
+    
+    delete mfun;
   }
 
   void testRemoveActive()
@@ -534,7 +536,8 @@ public:
     TS_ASSERT_EQUALS(mfun->getParameter(9),104);
     TS_ASSERT_EQUALS(mfun->getParameter(10),3.2);
     TS_ASSERT_EQUALS(mfun->getParameter(11),105);
-
+
+    delete mfun;
   }
 
   void testApplyTies()
@@ -595,7 +598,8 @@ public:
     TS_ASSERT_EQUALS(mfun->getParameter(9),3.1);
     TS_ASSERT_EQUALS(mfun->getParameter(10),79.1);
     TS_ASSERT_EQUALS(mfun->getParameter(11),3.3);
-
+
+    delete mfun;
   }
 
   void testApplyTiesInWrongOrder()
@@ -656,7 +660,8 @@ public:
     TS_ASSERT_EQUALS(mfun->getParameter(9),3.1);
     TS_ASSERT_EQUALS(mfun->getParameter(10),79.1);
     TS_ASSERT_EQUALS(mfun->getParameter(11),3.3);
-
+
+    delete mfun;
   }
 
   void testRemoveFunction()
@@ -773,7 +778,8 @@ public:
     TS_ASSERT(   mfun->isActive(5));
     TS_ASSERT( ! mfun->isActive(6));
     TS_ASSERT(   mfun->isActive(7));
-
+
+    delete mfun;
   }
 
   // replacing function has fewer parameters
@@ -1141,7 +1147,8 @@ public:
     TS_ASSERT_EQUALS(mfun->getParameter("f1.c"),1.1);
     TS_ASSERT_EQUALS(mfun->getParameter("f1.h"),0.2);
     TS_ASSERT_EQUALS(mfun->getParameter("f1.s"),1.33);
-
+
+    delete mfun;
   }
 
   void testRemoveFunctionWithTies()
@@ -1180,7 +1187,8 @@ public:
     TS_ASSERT_EQUALS(mfun->getParameter("f0.c"),1.1);
     TS_ASSERT_EQUALS(mfun->getParameter("f0.h"),1.2);
     TS_ASSERT_EQUALS(mfun->getParameter("f0.s"),0.3);
-
+
+    delete mfun;
   }
 
 private:
diff --git a/Code/Mantid/API/test/FunctionFactoryTest.h b/Code/Mantid/API/test/FunctionFactoryTest.h
index bdf2b749bd1..5eba358e591 100644
--- a/Code/Mantid/API/test/FunctionFactoryTest.h
+++ b/Code/Mantid/API/test/FunctionFactoryTest.h
@@ -202,7 +202,8 @@ public:
     TS_ASSERT_EQUALS(cf->getParameter(0),0.1);
     TS_ASSERT_EQUALS(cf->getParameter(1),1.1);
     TS_ASSERT_EQUALS(cf->getParameter(2),0.2);
-    TS_ASSERT_EQUALS(cf->getParameter(3),1.2);
+    TS_ASSERT_EQUALS(cf->getParameter(3),1.2);
+    delete fun;
   }
 
   void testCreateComposite1()
@@ -221,7 +222,8 @@ public:
     TS_ASSERT_EQUALS(cf->getParameter(0),0.);
     TS_ASSERT_EQUALS(cf->getParameter(1),0.);
     TS_ASSERT_EQUALS(cf->getParameter(2),0.2);
-    TS_ASSERT_EQUALS(cf->getParameter(3),1.2);
+    TS_ASSERT_EQUALS(cf->getParameter(3),1.2);
+    delete fun;
   }
 
   void testCreateComposite2()
@@ -242,7 +244,8 @@ public:
     TS_ASSERT_EQUALS(cf->getParameter(1),0.);
     TS_ASSERT_EQUALS(cf->getParameter(2),0.2);
     TS_ASSERT_EQUALS(cf->getParameter(3),1.2);
-    TS_ASSERT_EQUALS(fun->name(),"FunctionFactoryTest_CompFunctB");
+    TS_ASSERT_EQUALS(fun->name(),"FunctionFactoryTest_CompFunctB");
+    delete fun;
   }
 
   void testCreateComposite3()
@@ -265,7 +268,8 @@ public:
     TS_ASSERT_EQUALS(cf->getParameter(3),1.2);
     TS_ASSERT_EQUALS(fun->name(),"FunctionFactoryTest_CompFunctA");
     TS_ASSERT(fun->hasAttribute("attr"));
-    TS_ASSERT_EQUALS(fun->getAttribute("attr"),"hello");
+    TS_ASSERT_EQUALS(fun->getAttribute("attr"),"hello");
+    delete fun;
   }
 
   void testCreateCompositeNested()
@@ -284,8 +288,10 @@ public:
     TS_ASSERT_EQUALS(cf->getFunction(0)->name(),"FunctionFactoryTest_CompFunctA");
     TS_ASSERT_EQUALS(cf->getFunction(1)->name(),"FunctionFactoryTest_CompFunctB");
     TS_ASSERT_EQUALS(dynamic_cast<CompositeFunction*>(cf->getFunction(0))->nFunctions(),2);
-    TS_ASSERT_EQUALS(dynamic_cast<CompositeFunction*>(cf->getFunction(1))->nFunctions(),2);
-  }
+    TS_ASSERT_EQUALS(dynamic_cast<CompositeFunction*>(cf->getFunction(1))->nFunctions(),2);
+    delete fun;
+  }
+  
   void testCreateWithConstraint()
   {
     std::string fnString = "name=FunctionFactoryTest_FunctA,a0=0.1(0<a0<0.2),a1=1.1";
@@ -605,7 +611,8 @@ public:
     TS_ASSERT_EQUALS(fun1->getParameter(2),28.);
     TS_ASSERT_EQUALS(fun1->getParameter(3),789);
 
-    delete fun;
+    delete fun;
+    delete fun1;
   }
 
 };
diff --git a/Code/Mantid/API/test/LogParserTest.h b/Code/Mantid/API/test/LogParserTest.h
index 45c606848f3..19b498f6fa6 100644
--- a/Code/Mantid/API/test/LogParserTest.h
+++ b/Code/Mantid/API/test/LogParserTest.h
@@ -80,7 +80,8 @@ public:
         TS_ASSERT_EQUALS(tp1->firstValue(),1);
         //TS_ASSERT_EQUALS(secondValue(p1),2);
         TS_ASSERT_EQUALS(tp1->lastValue(),9);
-
+        
+        delete p1;
     }
 
     void testLate()
@@ -113,7 +114,8 @@ public:
         TS_ASSERT_EQUALS(ti->tm_hour, 14);
         TS_ASSERT_EQUALS(ti->tm_min, 3);
         TS_ASSERT_DELTA(timeMean(p1),8.0818, 0.001);
-
+        
+        delete p1;
     }
 
     void testEarly()
@@ -147,6 +149,7 @@ public:
         TS_ASSERT_EQUALS(ti->tm_min, 23);
         TS_ASSERT_DELTA(timeMean(p1),4.7096, 0.001);
 
+        delete p1;
     }
 
     void testSingle()
@@ -168,6 +171,7 @@ public:
         TS_ASSERT_EQUALS(ti->tm_min, 22);
         TS_ASSERT_DELTA(timeMean(p1),4., 0.001);
 
+        delete p1;
     }
 
     void testStr()
@@ -199,7 +203,7 @@ public:
         TS_ASSERT_EQUALS(ti->tm_hour, 14);
         TS_ASSERT_EQUALS(ti->tm_min, 3);
         // assert_throws(timeMean(p1));
-
+        delete p1;
     }
 
     void testNoICPevent()
@@ -230,6 +234,7 @@ public:
         TS_ASSERT_EQUALS(ti->tm_min, 3);
         TS_ASSERT_DELTA(timeMean(p1),8.0756, 0.001);
 
+        delete p1;
     }
 //*/
 private:
diff --git a/Code/Mantid/API/test/MatrixWorkspaceTest.h b/Code/Mantid/API/test/MatrixWorkspaceTest.h
index 8c8cdca8dd9..82f58710836 100644
--- a/Code/Mantid/API/test/MatrixWorkspaceTest.h
+++ b/Code/Mantid/API/test/MatrixWorkspaceTest.h
@@ -123,6 +123,7 @@ public:
   {
     Axis* axBad = new Axis(AxisType::Spectra,5);
     TS_ASSERT_THROWS( ws->replaceAxis(0,axBad), std::runtime_error )
+    delete axBad;
     Axis* ax = new Axis(AxisType::Spectra,1);
     TS_ASSERT_THROWS( ws->replaceAxis(1,ax), Exception::IndexError )
     TS_ASSERT_THROWS_NOTHING( ws->replaceAxis(0,ax) )
@@ -182,6 +183,8 @@ public:
     TS_ASSERT_EQUALS( ws2->maskedBins(0).rbegin()->first, 1 )
     TS_ASSERT_EQUALS( ws2->maskedBins(0).rbegin()->second, 0.5 )
     TS_ASSERT_EQUALS( ws2->dataY(0)[1], 0.25 )
+    
+    delete ws2;
   }
   
 private:
diff --git a/Code/Mantid/API/test/ParameterReferenceTest.h b/Code/Mantid/API/test/ParameterReferenceTest.h
index 5eb634985e7..e4f9c19cf93 100644
--- a/Code/Mantid/API/test/ParameterReferenceTest.h
+++ b/Code/Mantid/API/test/ParameterReferenceTest.h
@@ -125,7 +125,8 @@ public:
     TS_ASSERT_EQUALS(r9.getIndex(),0);
     TS_ASSERT_EQUALS(r10.getIndex(),1);
     TS_ASSERT_EQUALS(r11.getIndex(),2);
-
+
+    delete cf;
   }
 
 };
diff --git a/Code/Mantid/API/test/runTests.sh b/Code/Mantid/API/test/runTests.sh
index ff7ea78ab0e..1dc73953863 100644
--- a/Code/Mantid/API/test/runTests.sh
+++ b/Code/Mantid/API/test/runTests.sh
@@ -26,10 +26,7 @@ echo
 
 echo "Compiling the test executable..."
 g++ -O0 -g3 -o runner.exe runner.cpp -I ../inc -I ../../Kernel/inc -I ../../Geometry/inc -I ../../../Third_Party/include \
-            -L ../../debug -L ../../Build -L ../../../Third_Party/lib/linux64 -L $OPENCASCADELIBS \
-            -lMantidAPI -lMantidKernel -lMantidGeometry -lPocoFoundation -lPocoUtil \
-	        -lboost_regex -lboost_signals -lmuparserd -lgsl -lgslcblas -lGL -lGLU -lgts \
-            -lTKernel -lTKBO -lTKPrim -lTKMesh
+            -L ../../debug -L ../../Build -lMantidAPI -lMantidCurveFitting
 echo
 
 echo "Running the tests..."
diff --git a/Code/Mantid/CurveFitting/src/Fit.cpp b/Code/Mantid/CurveFitting/src/Fit.cpp
index 8ece75356a9..cea31902f80 100644
--- a/Code/Mantid/CurveFitting/src/Fit.cpp
+++ b/Code/Mantid/CurveFitting/src/Fit.cpp
@@ -360,21 +360,22 @@ namespace CurveFitting
 
     // check if derivative defined in derived class
     bool isDerivDefined = true;
-    try
-    {
       const std::vector<double> inTest(nActive(),1.0);
       std::vector<double> outTest(nActive());
       const double xValuesTest = 0;
       JacobianImpl1 J;
-      boost::shared_ptr<gsl_matrix> M( gsl_matrix_alloc(1,nActive()) );
-      J.setJ(M.get());
+      gsl_matrix* M( gsl_matrix_alloc(1,nActive()) );
+      J.setJ(M);
+      try
+      {
       // note nData set to zero (last argument) hence this should avoid further memory problems
-      functionDeriv(NULL, &J, &xValuesTest, 0);  
+      functionDeriv(NULL, &J, &xValuesTest, 0);
     }
     catch (Exception::NotImplementedError&)
     {
       isDerivDefined = false;
     }
+    gsl_matrix_free(M);
 
     // What minimizer to use
     std::string methodUsed = getProperty("Minimizer");
@@ -629,7 +630,7 @@ namespace CurveFitting
         // take standard deviations to be the square root of the diagonal elements of
         // the covariance matrix
         int iPNotFixed = 0;
-        for(size_t i=0; i < m_function->nParams(); i++)
+        for(int i=0; i < m_function->nParams(); i++)
         {
           standardDeviations.push_back(1.0);
           if (m_function->isActive(i))
@@ -652,7 +653,7 @@ namespace CurveFitting
         Mantid::API::ITableWorkspace_sptr m_covariance = Mantid::API::WorkspaceFactory::Instance().createTable("TableWorkspace");
         m_covariance->addColumn("str","Name");
         std::vector<std::string> paramThatAreFitted; // used for populating 1st "name" column
-        for(size_t i=0; i < m_function->nParams(); i++) 
+        for(int i=0; i < m_function->nParams(); i++) 
         {
           if (m_function->isActive(i)) 
           {
@@ -699,7 +700,7 @@ namespace CurveFitting
       //Mantid::API::TableRow row = m_result->appendRow();
       //row << "Chi^2/DoF" << finalCostFuncVal;
 
-      for(size_t i=0;i<m_function->nParams();i++)
+      for(int i=0;i<m_function->nParams();i++)
       {
         Mantid::API::TableRow row = m_result->appendRow();
         row << m_function->parameterName(i) << m_function->getParameter(i);
@@ -766,7 +767,8 @@ namespace CurveFitting
     delete [] l_data.sqrtWeightData;
     delete [] l_data.holdCalculatedData;
     gsl_matrix_free (l_data.holdCalculatedJacobian);
-
+    gsl_vector_free (initFuncArg);
+    
     return;
   }
 
diff --git a/Code/Mantid/CurveFitting/src/Fit1D.cpp b/Code/Mantid/CurveFitting/src/Fit1D.cpp
index a8c679ef40e..a637e9460e6 100644
--- a/Code/Mantid/CurveFitting/src/Fit1D.cpp
+++ b/Code/Mantid/CurveFitting/src/Fit1D.cpp
@@ -281,10 +281,11 @@ void Fit1D::exec()
     std::vector<double> outTest(m_parameterNames.size());
     const double xValuesTest = 0;
     JacobianImpl J;
-    boost::shared_ptr<gsl_matrix> M( gsl_matrix_alloc(m_parameterNames.size(),1) );
-    J.setJ(M.get());
+    gsl_matrix* M( gsl_matrix_alloc(m_parameterNames.size(),1) );
+    J.setJ(M);
     // note nData set to zero (last argument) hence this should avoid further memory problems
-    functionDeriv(&(inTest.front()), &J, &xValuesTest, 0);  
+    functionDeriv(&(inTest.front()), &J, &xValuesTest, 0);
+    gsl_matrix_free(M);
   }
   catch (Exception::NotImplementedError&)
   {
@@ -708,6 +709,7 @@ void Fit1D::exec()
   delete [] l_data.sigmaData;
   delete [] l_data.forSimplexLSwrap;
   delete [] l_data.parameters;
+  gsl_vector_free (initFuncArg);
 
   return;
 }
diff --git a/Code/Mantid/CurveFitting/test/ResolutionTest.h b/Code/Mantid/CurveFitting/test/ResolutionTest.h
index 75634a984b1..a90ca75dc77 100644
--- a/Code/Mantid/CurveFitting/test/ResolutionTest.h
+++ b/Code/Mantid/CurveFitting/test/ResolutionTest.h
@@ -149,8 +149,7 @@ void tearDown()
     const int nX = 100;
     const int nY = nX - 1;
 
-    Mantid::DataObjects::Workspace2D_sptr ws = boost::dynamic_pointer_cast<Mantid::DataObjects::Workspace2D>
-      (WorkspaceFactory::Instance().create("Workspace2D",1,nX,nY));
+    MatrixWorkspace_sptr ws = boost::dynamic_pointer_cast<MatrixWorkspace>(WorkspaceFactory::Instance().create("Workspace2D",1,nX,nY));
 
     double spec;
     double x;
diff --git a/Code/Mantid/CurveFitting/test/runTests.sh b/Code/Mantid/CurveFitting/test/runTests.sh
index 865aaee5836..acf01d90ffc 100755
--- a/Code/Mantid/CurveFitting/test/runTests.sh
+++ b/Code/Mantid/CurveFitting/test/runTests.sh
@@ -28,10 +28,8 @@ echo
 echo "Compiling the test executable..."
 g++ -O0 -g3 -o runner.exe runner.cpp -I ../inc -I ../../Kernel/inc -I ../../API/inc -I ../../DataObjects/inc -I ../../Geometry/inc \
 	                         -I ../../DataHandling/inc -I ../../Algorithms/inc -I ../../Nexus/inc -I ../../../Third_Party/include \
-	                         -L ../../debug -L ../../Build -L ../../../Third_Party/lib/linux64 -L $OPENCASCADELIBS \
-	                         -lMantidCurveFitting -lMantidKernel -lMantidGeometry -lMantidAPI -lMantidDataObjects -lMantidDataHandling -lMantidNexus -lMantidAlgorithms \
-	                         -lPocoFoundation -lPocoUtil -lboost_date_time -lboost_regex -lboost_signals \
-	                         -lmuparserd -lgsl -lgslcblas
+	                         -L ../../debug -L ../../Build \
+	                         -lMantidCurveFitting -lMantidKernel -lMantidGeometry -lMantidAPI -lMantidDataObjects -lMantidDataHandling -lMantidNexus -lMantidAlgorithms
 echo
 
 echo "Running the tests..."
diff --git a/Code/Mantid/DataHandling/inc/MantidDataHandling/SaveSPE.h b/Code/Mantid/DataHandling/inc/MantidDataHandling/SaveSPE.h
index a1eecbf7e13..7e7940ecc77 100755
--- a/Code/Mantid/DataHandling/inc/MantidDataHandling/SaveSPE.h
+++ b/Code/Mantid/DataHandling/inc/MantidDataHandling/SaveSPE.h
@@ -43,8 +43,8 @@ namespace DataHandling
 class DLLExport SaveSPE : public API::Algorithm
 {
 public:
-  /// (Empty) Constructor
-  SaveSPE() : API::Algorithm(), m_remainder(-1), m_nBins(-1) {}
+  /// Constructor
+  SaveSPE();
   /// Virtual destructor
   virtual ~SaveSPE() {}
   /// Algorithm's name
@@ -53,12 +53,12 @@ public:
   virtual const int version() const { return (1); }
   /// Algorithm's category for identification
   virtual const std::string category() const { return "DataHandling"; }
-  ///
+
 private:
-  ///the number of bins in each histogram, as the histogram must have common bins this shouldn't change
-  int m_nBins;
-  ///the SPE files have a constant number of numbers writen on each line, but depending on the number of bins there will be some "spare" numbers at the end of the block, this holds that number of spares
-  int m_remainder;
+  /// Initialisation code
+  void init();
+  ///Execution code
+  void exec();
 
   void writeHists(const API::MatrixWorkspace_const_sptr WS, FILE * const outFile);
   void writeHist(const API::MatrixWorkspace_const_sptr WS, FILE * const outFile, const int specIn) const;
@@ -66,12 +66,12 @@ private:
   void writeBins(const MantidVec &Vs, FILE * const outFile) const;
   void writeValue(const double value, FILE * const outFile) const;
   void logMissingMasked(const std::vector<int> &inds, const int nonMasked, const int masked) const;
-  /// Initialisation code
-  void init();
-  ///Execution code
-  void exec();
+  
+  ///the SPE files have a constant number of numbers writen on each line, but depending on the number of bins there will be some "spare" numbers at the end of the block, this holds that number of spares
+  int m_remainder;
+  ///the number of bins in each histogram, as the histogram must have common bins this shouldn't change
+  int m_nBins;
 
-protected:
   /// the mask flag (=-1e30) from the SPE specification http://www.mantidproject.org/images/3/3d/Spe_file_format.pdf
   static const double MASK_FLAG;
   /// the error value (=0.0) for spectra whose detectors are all masked, from the SPE specification http://www.mantidproject.org/images/3/3d/Spe_file_format.pdf
diff --git a/Code/Mantid/DataHandling/src/SaveSPE.cpp b/Code/Mantid/DataHandling/src/SaveSPE.cpp
index 17200938b78..50a5cfc573e 100755
--- a/Code/Mantid/DataHandling/src/SaveSPE.cpp
+++ b/Code/Mantid/DataHandling/src/SaveSPE.cpp
@@ -31,6 +31,8 @@ static const int NUM_PER_LINE = 8;
 const double SaveSPE::MASK_FLAG=-1e30;
 const double SaveSPE::MASK_ERROR=0.0;
 
+SaveSPE::SaveSPE() : API::Algorithm(), m_remainder(-1), m_nBins(-1) {}
+
 //---------------------------------------------------
 // Private member functions
 //---------------------------------------------------
diff --git a/Code/Mantid/DataHandling/test/runTests.sh b/Code/Mantid/DataHandling/test/runTests.sh
index e762560d206..84000d5ae4d 100644
--- a/Code/Mantid/DataHandling/test/runTests.sh
+++ b/Code/Mantid/DataHandling/test/runTests.sh
@@ -31,12 +31,8 @@ echo "Compiling the test executable..."
 # -lboost_filesystem added for the SaveCSVTest test
 g++ -O0 -g3 -o runner.exe runner.cpp -I ../inc -I ../../Kernel/inc -I ../../API/inc -I ../../DataObjects/inc \
             -I ../../Geometry/inc  -I ../../../Third_Party/include \
-            -L../../debug -L../../Build -L../../../Third_Party/lib/linux64 -L $OPENCASCADELIBS \
-            -lMantidDataHandling -lMantidKernel -lMantidGeometry -lMantidAPI -lMantidDataObjects \
-            -lPocoFoundation -lPocoUtil \
-            -lboost_regex -lboost_signals -lboost_date_time \
-            -lmuparserd -lgsl -lgslcblas -lNeXus -lGL -lGLU -lgts \
-            -lTKernel -lTKBO -lTKPrim -lTKMesh
+            -L../../debug -L../../Build \
+            -lMantidDataHandling -lMantidKernel -lMantidGeometry -lMantidAPI -lMantidDataObjects
 echo
 
 
diff --git a/Code/Mantid/DataObjects/src/TableWorkspace.cpp b/Code/Mantid/DataObjects/src/TableWorkspace.cpp
index f5cda999c21..49a01d18c38 100644
--- a/Code/Mantid/DataObjects/src/TableWorkspace.cpp
+++ b/Code/Mantid/DataObjects/src/TableWorkspace.cpp
@@ -17,8 +17,10 @@ namespace Mantid
     Kernel::Logger& TableWorkspace::g_log = Kernel::Logger::get("TableWorkspace");
 
     /// Constructor
-    TableWorkspace::TableWorkspace(int nrows)
-    {setRowCount(nrows);}
+    TableWorkspace::TableWorkspace(int nrows) : ITableWorkspace(), m_rowCount(0)
+    {
+      setRowCount(nrows);
+    }
 
     ///Destructor
     TableWorkspace::~TableWorkspace()
diff --git a/Code/Mantid/DataObjects/test/CompressedWorkspace2DTest.h b/Code/Mantid/DataObjects/test/CompressedWorkspace2DTest.h
index 4891e6f6ad3..762ef82bd5f 100644
--- a/Code/Mantid/DataObjects/test/CompressedWorkspace2DTest.h
+++ b/Code/Mantid/DataObjects/test/CompressedWorkspace2DTest.h
@@ -66,6 +66,7 @@ public:
     CompressedWorkspace2D *ws = new CompressedWorkspace2D;
     TS_ASSERT( dynamic_cast<Workspace2D*>(ws) )
     TS_ASSERT( dynamic_cast<Mantid::API::Workspace*>(ws) )
+    delete ws;
   }
 
   void testId()
diff --git a/Code/Mantid/DataObjects/test/ManagedWorkspace2DTest.h b/Code/Mantid/DataObjects/test/ManagedWorkspace2DTest.h
index d1914adb69e..3cdb244a929 100644
--- a/Code/Mantid/DataObjects/test/ManagedWorkspace2DTest.h
+++ b/Code/Mantid/DataObjects/test/ManagedWorkspace2DTest.h
@@ -74,6 +74,7 @@ public:
     ManagedWorkspace2D *ws = new ManagedWorkspace2D;
     TS_ASSERT( dynamic_cast<Workspace2D*>(ws) )
     TS_ASSERT( dynamic_cast<Mantid::API::Workspace*>(ws) )
+    delete ws;
   }
 
   void testId()
diff --git a/Code/Mantid/DataObjects/test/RefAxisTest.h b/Code/Mantid/DataObjects/test/RefAxisTest.h
index 98af94c9087..f5c317a77b9 100644
--- a/Code/Mantid/DataObjects/test/RefAxisTest.h
+++ b/Code/Mantid/DataObjects/test/RefAxisTest.h
@@ -33,9 +33,10 @@ public:
       a[i]=i+0.1;
     }
     for (int j = 0; j < 5; ++j) {
-      space->dataX(j) = *( new Mantid::MantidVec(a+(5*j), a+(5*j)+5) );
+      space->dataX(j) = Mantid::MantidVec(a+(5*j), a+(5*j)+5);
     }
-    delete a,b;
+    delete[] a;
+    delete[] b;
     
     // Create the axis that the tests will be performed on
     refAxis = new RefAxis(5, space);
@@ -46,7 +47,8 @@ public:
   ~RefAxisTest()
   {
     delete refAxis;
-    delete space, space2;
+    delete space;
+    delete space2;
   }
   
   void testConstructor()
@@ -68,6 +70,7 @@ public:
     TS_ASSERT( clonedAxis->isNumeric() )
     TS_ASSERT_EQUALS( (*clonedAxis)(0,0), 0.0 )
     TS_ASSERT_THROWS( (*clonedAxis)(0,1), std::range_error )
+    delete clonedAxis;
   }
 
   void testOperatorBrackets()
diff --git a/Code/Mantid/DataObjects/test/WorkspaceValidatorsTest.h b/Code/Mantid/DataObjects/test/WorkspaceValidatorsTest.h
index 3bd119ca9fb..42ecf0003c7 100644
--- a/Code/Mantid/DataObjects/test/WorkspaceValidatorsTest.h
+++ b/Code/Mantid/DataObjects/test/WorkspaceValidatorsTest.h
@@ -169,6 +169,7 @@ public:
     IValidator<MatrixWorkspace_sptr> *v = compVal.clone();
     TS_ASSERT_DIFFERS( v, &compVal )
     TS_ASSERT( dynamic_cast<CompositeValidator<>*>(v) )
+    delete v;
   }
 
   void testCompositeValidator_isValidandAdd()
diff --git a/Code/Mantid/DataObjects/test/runTests.sh b/Code/Mantid/DataObjects/test/runTests.sh
index 158b6f898f0..9d0dcae6195 100755
--- a/Code/Mantid/DataObjects/test/runTests.sh
+++ b/Code/Mantid/DataObjects/test/runTests.sh
@@ -11,6 +11,9 @@
 # Author: Russell Taylor, 07/11/07
 #
 
+# Remove old test build
+rm -rf runner.*
+
 echo "Generating the source file from the test header files..."
 # Chaining all tests together can have effects that you don't think of
 #  - it's always a good idea to run your new/changed test on its own
@@ -24,10 +27,8 @@ echo
 echo "Compiling the test executable..."
 g++ -O0 -g3 -o runner.exe runner.cpp -I ../inc -I ../../Kernel/inc -I ../../API/inc -I ../../Geometry/inc \
             -I ../../../Third_Party/include \
-            -L ../../debug -L ../../Build -L ../../../Third_Party/lib/linux64 -L $OPENCASCADELIBS \
-            -lMantidDataObjects -lMantidKernel -lMantidGeometry -lMantidAPI -lPocoFoundation -lPocoUtil \
-	        -lboost_regex -lboost_signals -lmuparserd -lgsl -lgslcblas -lGL -lGLU -lgts \
-            -lTKernel -lTKBO -lTKPrim -lTKMesh
+            -L ../../debug -L ../../Build \
+            -lMantidDataObjects
 echo
 
 echo "Running the tests..."
@@ -38,7 +39,6 @@ echo
 # Remove the generated files to ensure that they're not inadvertently run
 #   when something in the chain has failed.
 echo "Cleaning up..."
-rm -rf runner.*
 rm -f *.properties
 rm -f Test.log
 echo "Done."
diff --git a/Code/Mantid/Geometry/test/runTests.sh b/Code/Mantid/Geometry/test/runTests.sh
index 9cc1502ea4e..1c566268a84 100755
--- a/Code/Mantid/Geometry/test/runTests.sh
+++ b/Code/Mantid/Geometry/test/runTests.sh
@@ -25,10 +25,7 @@ echo
 
 echo "Compiling the test executable..."
 g++ -O0 -g3 -o runner.exe runner.cpp -I ../inc -I ../../Kernel/inc -I ../../../Third_Party/include \
-            -L ../../debug -L ../../Build -L ../../../Third_Party/lib/linux64 -L $OPENCASCADELIBS \
-            -lMantidGeometry -lMantidKernel -lPocoFoundation -lPocoUtil \
-            -lboost_regex -lboost_signals -lgsl -lgslcblas -lGL -lGLU -lgts \
-            -lTKernel -lTKBO -lTKPrim -lTKMesh
+            -L ../../debug -L ../../Build -lMantidGeometry -lMantidKernel
 echo
 
 echo "Running the tests..."
diff --git a/Code/Mantid/Kernel/test/FilePropertyTest.h b/Code/Mantid/Kernel/test/FilePropertyTest.h
index 7c0f2a0655d..20db5ae9bec 100644
--- a/Code/Mantid/Kernel/test/FilePropertyTest.h
+++ b/Code/Mantid/Kernel/test/FilePropertyTest.h
@@ -72,6 +72,7 @@ public:
     msg = fp->setValue("GEM38371.raw");
     TS_ASSERT_EQUALS(msg, "")    
 
+    delete fp;
   }
 
   void testSaveProperty()
@@ -84,6 +85,8 @@ public:
     //Test for some random file name as this doesn't need to exist here
     std::string msg = fp->setValue("filepropertytest.sav");
     TS_ASSERT_EQUALS(msg, "")
+    
+    delete fp;
   }
 
 };
diff --git a/Code/Mantid/Kernel/test/LogFilterTest.h b/Code/Mantid/Kernel/test/LogFilterTest.h
index 0a550b46104..57ee3822270 100644
--- a/Code/Mantid/Kernel/test/LogFilterTest.h
+++ b/Code/Mantid/Kernel/test/LogFilterTest.h
@@ -22,7 +22,12 @@ public:
         p->addValue("2007-11-30T16:17:30",4);
         p->addValue("2007-11-30T16:17:40",5);
     }
-
+
+    ~LogFilterTest()
+    {
+      delete p;
+    }
+    
     void testnthValue()
     {
         TS_ASSERT_EQUALS( p->size(), 5 )
diff --git a/Code/Mantid/Kernel/test/PropertyTest.h b/Code/Mantid/Kernel/test/PropertyTest.h
index aa06b8b821c..9d4cf47f964 100644
--- a/Code/Mantid/Kernel/test/PropertyTest.h
+++ b/Code/Mantid/Kernel/test/PropertyTest.h
@@ -27,6 +27,11 @@ public:
     p = new PropertyHelper;
   }
 
+  ~PropertyTest()
+  {
+    delete p;
+  }
+  
   void testName()
   {
     TS_ASSERT( ! p->name().compare("Test") )
diff --git a/Code/Mantid/Kernel/test/TimeSeriesPropertyTest.h b/Code/Mantid/Kernel/test/TimeSeriesPropertyTest.h
index 0efaefc9d87..bbdb4aa3112 100644
--- a/Code/Mantid/Kernel/test/TimeSeriesPropertyTest.h
+++ b/Code/Mantid/Kernel/test/TimeSeriesPropertyTest.h
@@ -17,6 +17,13 @@ public:
     sProp = new TimeSeriesProperty<std::string>("stringProp");
   }
   
+  ~TimeSeriesPropertyTest()
+  {
+    delete iProp;
+    delete dProp;
+    delete sProp;
+  }
+  
 	void testConstructor()
 	{
     // Test that all the base class member variables are correctly assigned to
diff --git a/Code/Mantid/Kernel/test/runTests.sh b/Code/Mantid/Kernel/test/runTests.sh
index 3a87d02bc2d..fbe9b02bb04 100755
--- a/Code/Mantid/Kernel/test/runTests.sh
+++ b/Code/Mantid/Kernel/test/runTests.sh
@@ -26,8 +26,7 @@ echo
 
 echo "Compiling the test executable..."
 g++ -O0 -g3 -o runner.exe runner.cpp -I ../inc -I ../../../Third_Party/include \
-            -L ../../debug -L ../../Build -L ../../../Third_Party/lib/linux64 \
-            -lMantidKernel -lPocoFoundationd -lPocoUtild -lboost_signals
+            -L ../../debug -L ../../Build -lMantidKernel
 echo
 
 echo "Running the tests..."
diff --git a/Code/Mantid/Nexus/src/LoadNexusProcessed.cpp b/Code/Mantid/Nexus/src/LoadNexusProcessed.cpp
index 8205ad74a2e..cad44557628 100644
--- a/Code/Mantid/Nexus/src/LoadNexusProcessed.cpp
+++ b/Code/Mantid/Nexus/src/LoadNexusProcessed.cpp
@@ -354,6 +354,7 @@ namespace Mantid
       }
 
       local_workspace->mutableSpectraMap().populate(spectra_list, det_list.get(), ndets);
+      delete[] spectra_list;
     }
 
     /**
diff --git a/Code/Mantid/Nexus/src/NexusClasses.cpp b/Code/Mantid/Nexus/src/NexusClasses.cpp
index f072dd7a346..8a359d5589e 100644
--- a/Code/Mantid/Nexus/src/NexusClasses.cpp
+++ b/Code/Mantid/Nexus/src/NexusClasses.cpp
@@ -310,7 +310,7 @@ std::vector< std::string >& NXNote::data()
       NXgetdata(m_fileID,buffer);
       NXclosedata(m_fileID);
       std::istringstream istr(std::string(buffer,n));
-      delete buffer;
+      delete[] buffer;
       /* end of new code */
 
       m_data.clear();
diff --git a/Code/Mantid/Nexus/test/runTests.sh b/Code/Mantid/Nexus/test/runTests.sh
index 6fea0fb79db..b2c9e96df44 100644
--- a/Code/Mantid/Nexus/test/runTests.sh
+++ b/Code/Mantid/Nexus/test/runTests.sh
@@ -25,10 +25,8 @@ echo
 
 echo "Compiling the test executable..."
 g++ -O0 -g3 -o runner.exe runner.cpp -I ../inc -I ../../Kernel/inc -I ../../API/inc -I ../../DataObjects/inc -I ../../DataHandling/inc -I ../../Geometry/inc  \
-            -I ../../../Third_Party/include -L../../debug -L../../Build -L../../../Third_Party/lib/linux64 -L $OPENCASCADELIBS \
-            -lMantidNexus -lMantidKernel -lMantidGeometry -lMantidAPI -lMantidDataObjects -lMantidDataHandling \
-            -lboost_date_time -lPocoFoundation -lPocoUtil \
-            -lboost_regex -lboost_signals -lmuparserd -lgsl -lgslcblas
+            -I ../../../Third_Party/include -L../../debug -L../../Build \
+            -lMantidNexus -lMantidKernel -lMantidGeometry -lMantidAPI -lMantidDataObjects -lMantidDataHandling
 echo
 
 
diff --git a/Code/Mantid/PythonAPI/test/runTests.sh b/Code/Mantid/PythonAPI/test/runTests.sh
index a5b9e08b979..085452a9576 100644
--- a/Code/Mantid/PythonAPI/test/runTests.sh
+++ b/Code/Mantid/PythonAPI/test/runTests.sh
@@ -26,12 +26,8 @@ echo
 echo "Compiling the test executable..."
 g++ -w -O0 -g3 -o runner.exe runner.cpp -I ../inc -I ../../Kernel/inc -I ../../API/inc -I ../../DataObjects/inc -I ../../Geometry/inc \
 	                         -I ../../../Third_Party/include -I /usr/include/python2.3 \
-	                         -L ../../debug -L ../../Build -L ../../../Third_Party/lib/linux64 -L $OPENCASCADELIBS \
-	                         -lMantidPythonAPI -lMantidKernel -lMantidGeometry -lMantidAPI -lMantidDataObjects \
-	                         -lPocoFoundation -lPocoUtil -lboost_python -lboost_regex \
-	                         -lboost_date_time -lboost_signals -lpython2.3 \
-	                         -lmuparserd -lgsl -lgslcblas -lGL -lGLU -lgts \
-				             -lTKernel -lTKBO -lTKPrim -lTKMesh
+	                         -L ../../debug -L ../../Build \
+	                         -lMantidPythonAPI
 echo
 
 echo "Running the tests..."
-- 
GitLab