my framework provides smartpointers. |
There are plenty of different "smart pointers". C++ standard library has now many, Boost have similar, your framework has something. You have to know the features of the types that are at your disposal.
When a std::vector object is destructed, all of its elements are destructed.
The elements in your example are std::shared_ptr objects.
When a std::shared_ptr object is destructed and it is the last copy holding the address in it, it will deallocate and destruct the object at the address.
In your example the line 7 creates a copy. The scope of objptr ends at line 8, so the objptr is destructed, but the copy in vec lives on, as does the dynamically allocated Object. When vec goes out of scope, so does the copy created from objptr, and unless you have made additional copies, the Object as well. Properly.
Note though that the following does not have copies:
1 2 3 4 5
|
{
Object * obj = new Object;
std::shared_ptr<Object> foo( obj );
std::shared_ptr<Object> bar( obj );
} // error: double deletion; foo is not a copy of bar
|