Skip to content

Commit

Permalink
ofParameterGroup: fix nested parameter groups and memory leak, switch…
Browse files Browse the repository at this point in the history
… ofPtr to shared_ptr.

Closes openframeworks#2674
  • Loading branch information
arturoc committed Jun 9, 2014
1 parent 309cf49 commit 0c90c15
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
8 changes: 4 additions & 4 deletions libs/openFrameworks/types/ofParameter.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ class ofParameter: public ofAbstractParameter{
bool bInNotify;
bool serializable;
};
ofPtr<Value> obj;
shared_ptr<Value> obj;
void (ofParameter<ParameterType>::*setMethod)(ParameterType v);

void eventsSetValue(ParameterType v);
Expand All @@ -181,17 +181,17 @@ ofParameter<ParameterType>::ofParameter()

template<typename ParameterType>
ofParameter<ParameterType>::ofParameter(ParameterType v)
:obj(ofPtr<Value>(new Value(v)))
:obj(shared_ptr<Value>(new Value(v)))
,setMethod(&ofParameter<ParameterType>::eventsSetValue) {}

template<typename ParameterType>
ofParameter<ParameterType>::ofParameter(string name, ParameterType v)
:obj(ofPtr<Value>(new Value(name, v)))
:obj(shared_ptr<Value>(new Value(name, v)))
,setMethod(&ofParameter<ParameterType>::eventsSetValue){}

template<typename ParameterType>
ofParameter<ParameterType>::ofParameter(string name, ParameterType v, ParameterType min, ParameterType max)
:obj(ofPtr<Value>(new Value(name, v, min, max)))
:obj(shared_ptr<Value>(new Value(name, v, min, max)))
,setMethod(&ofParameter<ParameterType>::eventsSetValue){}


Expand Down
11 changes: 6 additions & 5 deletions libs/openFrameworks/types/ofParameterGroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ ofParameterGroup::ofParameterGroup()

}

void ofParameterGroup::add(ofAbstractParameter & param){
obj->parameters.push_back(&param);
obj->parametersIndex[param.getEscapedName()] = obj->parameters.size()-1;
param.setParent(this);
void ofParameterGroup::add(ofParameterGroup & param){
shared_ptr<ofParameterGroup> group(new ofParameterGroup(param));
obj->parameters.push_back(group);
obj->parametersIndex[group->getEscapedName()] = obj->parameters.size()-1;
group->setParent(this);
}

void ofParameterGroup::clear(){
Expand Down Expand Up @@ -125,7 +126,7 @@ ofParameterGroup ofParameterGroup::getGroup(int pos) const{
return ofParameterGroup();
}else{
if(getType(pos)==typeid(ofParameterGroup).name()){
return *static_cast<ofParameterGroup* >(obj->parameters[pos]);
return *static_cast<ofParameterGroup* >(obj->parameters[pos].get());
}else{
ofLogError("ofParameterGroup") << "get(): bad type for pos " << pos << ", returning empty group";
return ofParameterGroup();
Expand Down
17 changes: 8 additions & 9 deletions libs/openFrameworks/types/ofParameterGroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@
#include "ofConstants.h"
#include "ofLog.h"
#include "ofParameter.h"
#include "ofTypes.h"

class ofParameterGroup: public ofAbstractParameter {
public:
ofParameterGroup();

template<typename ParameterType>
void add(ofParameter<ParameterType> param);
void add(ofParameter<ParameterType> & param);

template<typename ParameterType,typename Friend>
void add(ofReadOnlyParameter<ParameterType,Friend> & param);

void add(ofAbstractParameter & param);
void add(ofParameterGroup & param);


void clear();
Expand Down Expand Up @@ -103,11 +104,11 @@ class ofParameterGroup: public ofAbstractParameter {
Value():serializable(true){}

map<string,int> parametersIndex;
vector<ofAbstractParameter*> parameters;
vector<shared_ptr<ofAbstractParameter> > parameters;
string name;
bool serializable;
};
ofPtr<Value> obj;
shared_ptr<Value> obj;
};


Expand All @@ -122,18 +123,16 @@ ofParameter<ParameterType> ofParameterGroup::get(int pos) const{
}

template<class ParameterType>
void ofParameterGroup::add(ofParameter<ParameterType> param){
ofParameter<ParameterType> * p = new ofParameter<ParameterType>;
p->makeReferenceTo(param);
void ofParameterGroup::add(ofParameter<ParameterType> & param){
shared_ptr<ofParameter<ParameterType> > p(new ofParameter<ParameterType>(param));
obj->parameters.push_back(p);
obj->parametersIndex[p->getEscapedName()] = obj->parameters.size()-1;
p->setParent(this);
}

template<typename ParameterType,typename Friend>
void ofParameterGroup::add(ofReadOnlyParameter<ParameterType,Friend> & param){
ofReadOnlyParameter<ParameterType,Friend> * p = new ofReadOnlyParameter<ParameterType,Friend>;
p->makeReferenceTo(param);
shared_ptr<ofReadOnlyParameter<ParameterType,Friend> > p(new ofReadOnlyParameter<ParameterType,Friend>(param));
obj->parameters.push_back(p);
obj->parametersIndex[p->getEscapedName()] = obj->parameters.size()-1;
p->setParent(this);
Expand Down

0 comments on commit 0c90c15

Please sign in to comment.