Skip to content

Commit

Permalink
Clean memory leaks from CoordTransformAffine
Browse files Browse the repository at this point in the history
Refs #11512
  • Loading branch information
martyngigg committed Apr 13, 2015
1 parent 677c0c6 commit d2e7482
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
10 changes: 8 additions & 2 deletions Code/Mantid/Framework/DataObjects/src/CoordTransformAffine.cpp
Expand Up @@ -246,7 +246,7 @@ std::string CoordTransformAffine::toXMLString() const {

AutoPtr<Element> coordTransformTypeElement = pDoc->createElement("Type");
coordTransformTypeElement->appendChild(
pDoc->createTextNode("CoordTransformAffine"));
AutoPtr<Node>(pDoc->createTextNode("CoordTransformAffine")));
coordTransformElement->appendChild(coordTransformTypeElement);

AutoPtr<Element> paramListElement = pDoc->createElement("ParameterList");
Expand Down Expand Up @@ -301,6 +301,7 @@ CoordTransformAffine::combineTransformations(CoordTransform *first,
"same as the # of input dimensions of second.");
// Convert both inputs to affine matrices, if needed
CoordTransformAffine *firstAff = dynamic_cast<CoordTransformAffine *>(first);
bool ownFirstAff(false);
if (!firstAff) {
CoordTransformAligned *firstAl =
dynamic_cast<CoordTransformAligned *>(first);
Expand All @@ -310,9 +311,11 @@ CoordTransformAffine::combineTransformations(CoordTransform *first,
"must be either CoordTransformAffine or CoordTransformAligned.");
firstAff = new CoordTransformAffine(firstAl->getInD(), firstAl->getOutD());
firstAff->setMatrix(firstAl->makeAffineMatrix());
ownFirstAff = true;
}
CoordTransformAffine *secondAff =
dynamic_cast<CoordTransformAffine *>(second);
bool ownSecondAff(false);
if (!secondAff) {
CoordTransformAligned *secondAl =
dynamic_cast<CoordTransformAligned *>(second);
Expand All @@ -323,15 +326,18 @@ CoordTransformAffine::combineTransformations(CoordTransform *first,
secondAff =
new CoordTransformAffine(secondAl->getInD(), secondAl->getOutD());
secondAff->setMatrix(secondAl->makeAffineMatrix());
ownSecondAff = true;
}
// Initialize the affine matrix
CoordTransformAffine *out =
new CoordTransformAffine(firstAff->getInD(), secondAff->getOutD());
// Multiply the two matrices together
Matrix<coord_t> outMat = secondAff->getMatrix() * firstAff->getMatrix();
std::cout << "Creating combined matrix " << outMat << std::endl;
// Set in the output
out->setMatrix(outMat);
// Clean up
if(ownFirstAff) delete firstAff;
if(ownSecondAff) delete secondAff;
return out;
}

Expand Down
Expand Up @@ -11,8 +11,8 @@
#include "MantidDataObjects/CoordTransformDistance.h"
#include "MantidDataObjects/MDEventFactory.h"
#include <cxxtest/TestSuite.h>
#include <iomanip>
#include <iostream>

#include <boost/scoped_ptr.hpp>

using namespace Mantid;
using namespace Mantid::Kernel;
Expand Down Expand Up @@ -113,7 +113,7 @@ class CoordTransformAffineTest : public CxxTest::TestSuite
ct.addTranslation(translation);

// Clone and check the clone works
CoordTransform * clone = ct.clone();
boost::scoped_ptr<CoordTransform> clone(ct.clone());
clone->apply(in, out);
compare(2, out, expected);
}
Expand Down Expand Up @@ -269,7 +269,9 @@ class CoordTransformAffineTest : public CxxTest::TestSuite
TSM_ASSERT_THROWS_ANYTHING("Null input fails.", CoordTransformAffine::combineTransformations(NULL, &ct43) );
TSM_ASSERT_THROWS_ANYTHING("Incompatible # of dimensions", CoordTransformAffine::combineTransformations(&ct42, &ct32) );
TSM_ASSERT_THROWS_ANYTHING("Incompatible # of dimensions", CoordTransformAffine::combineTransformations(&ct32, &ct43) );
TSM_ASSERT_THROWS_NOTHING("Compatible # of dimensions", CoordTransformAffine::combineTransformations(&ct43, &ct32) );
CoordTransformAffine *ct(NULL);
TSM_ASSERT_THROWS_NOTHING("Compatible # of dimensions", ct = CoordTransformAffine::combineTransformations(&ct43, &ct32) );
delete ct;
coord_t center[3] = {1,2,3};
bool bools[3] = {true, true, true};
CoordTransformDistance ctd(3, center, bools);
Expand All @@ -291,7 +293,7 @@ class CoordTransformAffineTest : public CxxTest::TestSuite
CoordTransformAffine ct2(2,2);
ct2.addTranslation(translation2);
// Combine them
CoordTransformAffine * combined = CoordTransformAffine::combineTransformations(&ct1, &ct2);
boost::scoped_ptr<CoordTransformAffine> combined(CoordTransformAffine::combineTransformations(&ct1, &ct2));
combined->apply(in, out);
compare(2, out, expected);
}
Expand All @@ -311,7 +313,7 @@ class CoordTransformAffineTest : public CxxTest::TestSuite
ct2->apply(out1, out2);

// Combine them
CoordTransformAffine * combined = CoordTransformAffine::combineTransformations(ct1, ct2);
boost::scoped_ptr<CoordTransformAffine> combined(CoordTransformAffine::combineTransformations(ct1, ct2));
combined->apply(in, out_combined);

// Applying the combined one = same as each one in sequence
Expand Down Expand Up @@ -434,4 +436,3 @@ class CoordTransformAffineTestPerformance : public CxxTest::TestSuite


#endif /* MANTID_DATAOBJECTS_COORDTRANSFORMAFFINETEST_H_ */

0 comments on commit d2e7482

Please sign in to comment.