c++ shared_ptr of stack object
You should probably define the semantics of your class more clearly. If you want RenderView to be the owner of the RenderModel it should create it on its own (maybe get in the constructor some identifier to use with a factory).
I've seen classes that receive ownership of objects, and it was defined explicitly that this objects must be on the heap, but this is, to my opinion, error prone, just like the error you now encountered. You can not give a stack object to a smart pointer that expects it to be on the heap (because it will use delete on it when it wants to clean it).
how do I dictate that a parameter was not allocated on the stack?
Yes, require the caller to provide a std::shared_ptr<RenderModel>
. If the caller misconstructs the std::shared_ptr
, that's the caller's problem, not yours.
If you intend for a RenderView
to be the sole owner of a particular RenderModel
, consider having the function take a std::unique_ptr
or std::auto_ptr
instead; this way it is clear that the caller should not retain ownership of the object after it calls the function.
Alternatively, if RenderModel
is cheap to copy, make a copy of it and use the copy:
_model.reset(new RenderModel(model));