Skip to content

Commit

Permalink
Fix spline data memory leak
Browse files Browse the repository at this point in the history
Fix memory leak with by use unique_ptr and make_unique
  • Loading branch information
RDelet committed Sep 17, 2024
1 parent d3f3075 commit 18ab387
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
11 changes: 6 additions & 5 deletions src/twistSplineData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ void* TwistSplineData::creator(){
return new TwistSplineData;
}

TwistSplineData::TwistSplineData() {
_twistSpline = new TwistSplineT();
TwistSplineData::TwistSplineData()
: _twistSpline(std::make_unique<TwistSplineT>())
{
}

TwistSplineData::~TwistSplineData() {
Expand All @@ -43,7 +44,7 @@ void TwistSplineData::copy(const MPxData& other) {
if (other.typeId() == TwistSplineData::id) {
const TwistSplineData* otherData = (const TwistSplineData*) & other;
const TwistSplineT * x = otherData->getSpline();
_twistSpline = new TwistSplineT(*x);
_twistSpline = std::make_unique<TwistSplineT>(*otherData->getSpline());
}
else {
// we need to convert to the other type based on its iff Tag
Expand All @@ -53,11 +54,11 @@ void TwistSplineData::copy(const MPxData& other) {
}

const TwistSplineT* TwistSplineData::getSpline() const {
return _twistSpline;
return _twistSpline.get();
}

TwistSplineT* TwistSplineData::getSpline() {
return _twistSpline;
return _twistSpline.get();
}

MTypeId TwistSplineData::typeId() const {
Expand Down
2 changes: 1 addition & 1 deletion src/twistSplineData.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@ class TwistSplineData : public MPxData {
static void* creator();

private:
TwistSplineT* _twistSpline;
std::unique_ptr<TwistSplineT> _twistSpline;
};

0 comments on commit 18ab387

Please sign in to comment.