title: c++ shared_ptr of stack object
body: <p>I have been learning managed pointers lately and ran into the following scenario.</p>
  
  <p>I am implementing a model/controller class for a game view. My view, will render things in the model. Pretty straight forward. In my main function, I instantiate all three like this:</p>
  
  <pre><code>RenderModel m;
  m.AddItem(rect); // rect gets added just fine, it's an "entity" derivee
  RenderView v;
  v.SetModel(m);
  </code></pre>
  
  <p>My render view class is pretty straightforward:</p>
  
  <pre><code>class RenderView
  {
  public:
  explicit RenderView();
  ~RenderView();
  
  void Update();
  
  void SetModel(RenderModel&amp; model);
  
  private:
  // disable
  RenderView(const RenderView&amp; other);
  RenderView&amp; operator=(const RenderView&amp; other);
  
  // private members
  boost::scoped_ptr&lt;RenderModel&gt; _model;
  };
  </code></pre>
  
  <p>The implementation for setView is pretty standard:</p>
  
  <pre><code>void RenderView::SetModel(RenderModel&amp; model)
  {
      _model.reset(&amp;model);
  }
  </code></pre>
  
  <p>The key to this is, the view stores a model in a smart pointer. However in main, the model was allocated on the stack. When the program exits, the memory gets deleted twice. This makes sense. My current understanding tells me that anything which gets stored in a smart_ptr (of any kind) should not have been allocated on the stack. </p>
  
  <p>After all the above setup, my question is simple: how do I dictate that a parameter was not allocated on the stack? Is accepting a smart pointer as a parameter the only solution? Even then I could not ensure that someone using my view class could not do something incorrect such as:</p>
  
  <pre><code>// If I implemented SetModel this way:
  void RenderView::SetModel(const std::shared_ptr&lt;RenderModel&gt;&amp; model)
  {
      _model.reset(&amp;*model);
  }
  
  RenderModel m;
  RenderView v;
  std::shared_ptr&lt;RenderModel&gt; ptr(&amp;m); // create a shared_ptr from a stack-object.
  v.SetModel(ptr);
  </code></pre>