lundi 23 février 2015

Segmentation fault when using a shared_ptr

I am making a particle system and I'm struggling with how to structure my code. The idea is that a user can create one or several ParticleEmitter objects that are passed to a ParticleManager object via the ofxCurlNoise object.


Now, I want that when the user updates the ParticleEmitters objects, the ParticleManager object sees the changes made. So I used shared pointers but I have segmentation faults at different times, whether I use one ParticleEmitter (segmentation fault when the program starts) or a vector<ParticleEmitter> (segmentation fault when the program exits).


What is wrong with this? Is there a design pattern for doing what I'm trying to do?


ofApp.h



#include "ofxCurlNoise.h"

class ofApp : public ofBaseApp{

// ParticleEmitter particleEmitter;
vector<ParticleEmitter> particleEmitters;
ofxCurlNoise curlNoise;

public:
void setup();

};


ofApp.cpp



#include "ofApp.h"

void ofApp::setup(){
// This produces a segfault as soon as the program starts
// particleEmitter.setup();
// curlNoise.setup(particleEmitter, 1024*256);

// This produces a segfault when the program exits
ParticleEmitter emitter;
emitter.setup();
particleEmitters.push_back(emitter);
curlNoise.setup(particleEmitters, 1024*256);

}


ofxCurlNoise.h



#include "ParticleManager.h"

class ofxCurlNoise {

ParticleManager particleManager;

public:
void setup(ParticleEmitter& emitter, int n);
void setup(vector<ParticleEmitter>& emitters, int n);

private:
void setup(int n);

};


ofxCurlNoise.cpp



#include "ofxCurlNoise.h"

void ofxCurlNoise::setup(ParticleEmitter& emitter, int n){
particleManager.addEmitter(shared_ptr<ParticleEmitter>(&emitter));
setup(n);
}

void ofxCurlNoise::setup(vector<ParticleEmitter>& emitters, int n){
for(auto& e : emitters){
particleManager.addEmitter(shared_ptr<ParticleEmitter>(&e));
}
setup(n);
}

void ofxCurlNoise::setup(int n){
particleManager.setup(n);
}


ParticleManager.h



#include "ParticleEmitter.h"

class ParticleManager{

vector<shared_ptr<ParticleEmitter>> emitters;

public:
void addEmitter(const shared_ptr<ParticleEmitter>& emitter);
void setup(int n);
};


ParticleManager.cpp



#include "ParticleManager.h"

void ParticleManager::setup(int n){
//...
}

void ParticleManager::addEmitter(const shared_ptr<ParticleEmitter>& emitter){
emitters.push_back(emitter);
}

Aucun commentaire:

Enregistrer un commentaire