Skip to content

Commit 0a13821

Browse files
authored
Merge pull request #3247 from opensim-org/fix_controller-setmemoryowner-memleak
Fixed OpenSim::Controller leaking memory whenever it is copied
2 parents 7702ead + cd4e406 commit 0a13821

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

OpenSim/Simulation/Control/Controller.cpp

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,50 @@ using namespace std;
4444
/**
4545
* Default constructor.
4646
*/
47-
Controller::Controller()
47+
Controller::Controller() :
48+
ModelComponent{},
49+
_numControls{0},
50+
_actuatorSet{}
4851
{
4952
constructProperties();
5053
}
5154

55+
Controller::Controller(Controller const& src) :
56+
ModelComponent{src},
57+
PropertyIndex_enabled{src.PropertyIndex_enabled},
58+
PropertyIndex_actuator_list{src.PropertyIndex_actuator_list},
59+
_numControls{src._numControls},
60+
_actuatorSet{}
61+
{
62+
// care: the reason this custom copy constructor exists is to prevent
63+
// a memory leak (#3247)
64+
_actuatorSet.setMemoryOwner(false);
65+
}
66+
67+
Controller& Controller::operator=(Controller const& src)
68+
{
69+
// care: the reason this custom copy assignment exists is to prevent
70+
// a memory leak (#3247)
71+
72+
if (&src != this)
73+
{
74+
static_cast<ModelComponent&>(*this) = static_cast<ModelComponent const&>(src);
75+
PropertyIndex_enabled = src.PropertyIndex_enabled;
76+
PropertyIndex_actuator_list = src.PropertyIndex_actuator_list;
77+
_numControls = src._numControls;
78+
_actuatorSet.setSize(0);
79+
}
80+
return *this;
81+
}
82+
83+
Controller::~Controller() noexcept = default;
5284

5385
//=============================================================================
5486
// CONSTRUCTION
5587
//=============================================================================
5688
//_____________________________________________________________________________
5789
//_____________________________________________________________________________
90+
5891
/**
5992
* Connect properties to local pointers.
6093
*/
@@ -63,8 +96,6 @@ void Controller::constructProperties()
6396
setAuthors("Ajay Seth, Frank Anderson, Chand John, Samuel Hamner");
6497
constructProperty_enabled(true);
6598
constructProperty_actuator_list();
66-
67-
// Set is only a reference list, not ownership
6899
_actuatorSet.setMemoryOwner(false);
69100
}
70101

@@ -125,8 +156,8 @@ void Controller::extendConnectToModel(Model& model)
125156
// if we use a list Socket<Actuator>
126157

127158
// make sure controller does not take ownership
128-
_actuatorSet.setMemoryOwner(false);
129159
_actuatorSet.setSize(0);
160+
_actuatorSet.setMemoryOwner(false);
130161

131162
int nac = getProperty_actuator_list().size();
132163
if (nac == 0)
@@ -172,9 +203,9 @@ void Controller::setActuators(const Set<Actuator>& actuators)
172203
//TODO this needs to be setting a Socket list of Actuators
173204

174205
// make sure controller does NOT assume ownership
206+
_actuatorSet.setSize(0);
175207
_actuatorSet.setMemoryOwner(false);
176208
//Rebuild consistent set of actuator lists
177-
_actuatorSet.setSize(0);
178209
updProperty_actuator_list().clear();
179210
for (int i = 0; i< actuators.getSize(); i++){
180211
addActuator(actuators[i]);

OpenSim/Simulation/Control/Controller.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ OpenSim_DECLARE_ABSTRACT_OBJECT(Controller, ModelComponent);
8181

8282
/** Default constructor. */
8383
Controller();
84+
Controller(Controller const&);
85+
Controller& operator=(Controller const&);
86+
~Controller() noexcept override;
8487

8588
// Uses default (compiler-generated) destructor, copy constructor and copy
8689
// assignment operator.
@@ -146,9 +149,6 @@ OpenSim_DECLARE_ABSTRACT_OBJECT(Controller, ModelComponent);
146149
// construct and initialize properties
147150
void constructProperties();
148151

149-
//friend class ControlSet;
150-
friend class ControllerSet;
151-
152152
//=============================================================================
153153
}; // END of class Controller
154154

0 commit comments

Comments
 (0)