title: Segmentation fault when using a shared_ptr
body: <p>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 <code>ParticleEmitter</code> objects that are passed to a <code>ParticleManager</code> object via the <code>ofxCurlNoise</code> object. </p>
  
  <p>Now, I want that when the user updates the <code>ParticleEmitters</code> objects, the <code>ParticleManager</code> object sees the changes made. So I used shared pointers but I have segmentation faults at different times, whether I use one <code>ParticleEmitter</code> (segmentation fault when the program starts) or a <code>vector&lt;ParticleEmitter&gt;</code> (segmentation fault when the program exits).</p>
  
  <p>What is wrong with this? Is there a design pattern for doing what I'm trying to do? </p>
  
  <p><strong>ofApp.h</strong></p>
  
  <pre><code>#include "ofxCurlNoise.h"
  
  class ofApp : public ofBaseApp{
  
      // ParticleEmitter particleEmitter;
      vector&lt;ParticleEmitter&gt; particleEmitters;
      ofxCurlNoise curlNoise;
  
      public:
          void setup();
  
  };
  </code></pre>
  
  <p><strong>ofApp.cpp</strong></p>
  
  <pre><code>#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);    
  
  }
  </code></pre>
  
  <p><strong>ofxCurlNoise.h</strong></p>
  
  <pre><code>#include "ParticleManager.h"
  
  class ofxCurlNoise {    
  
      ParticleManager particleManager;
  
      public:
          void setup(ParticleEmitter&amp; emitter, int n);
          void setup(vector&lt;ParticleEmitter&gt;&amp; emitters, int n);
  
      private:
          void setup(int n);    
  
  };
  </code></pre>
  
  <p><strong>ofxCurlNoise.cpp</strong></p>
  
  <pre><code>#include "ofxCurlNoise.h"
  
  void ofxCurlNoise::setup(ParticleEmitter&amp; emitter, int n){
      particleManager.addEmitter(shared_ptr&lt;ParticleEmitter&gt;(&amp;emitter));
      setup(n);
  }
  
  void ofxCurlNoise::setup(vector&lt;ParticleEmitter&gt;&amp; emitters, int n){
      for(auto&amp; e : emitters){
          particleManager.addEmitter(shared_ptr&lt;ParticleEmitter&gt;(&amp;e));
      }
      setup(n);
  }
  
  void ofxCurlNoise::setup(int n){
      particleManager.setup(n);
  }
  </code></pre>
  
  <p><strong>ParticleManager.h</strong></p>
  
  <pre><code>#include "ParticleEmitter.h"
  
  class ParticleManager{    
  
      vector&lt;shared_ptr&lt;ParticleEmitter&gt;&gt; emitters;
  
      public:
          void addEmitter(const shared_ptr&lt;ParticleEmitter&gt;&amp; emitter);
          void setup(int n);
  };
  </code></pre>
  
  <p><strong>ParticleManager.cpp</strong></p>
  
  <pre><code>#include "ParticleManager.h"
  
  void ParticleManager::setup(int n){
      //...
  }
  
  void ParticleManager::addEmitter(const shared_ptr&lt;ParticleEmitter&gt;&amp; emitter){
      emitters.push_back(emitter);
  }
  </code></pre>