Reputation: 349
Will the smart pointer or scoped pointers delete an object when the class has no destructor If not, why not just leave the scope and let the object be deleted by itself?
Upvotes: 1
Views: 4041
Reputation: 153899
Your point is well taken; there aren't that many uses for smart pointers
in C++, since most of the time where they might be relevant, you'd be
better off using value semantics, and copying. In the case of
scoped_ptr
, the most frequent use is when the actual object is
polymorphic:
scoped_ptr<Base> pObj = condition
? static_cast<Base*>( new Derived1 )
: static_cast<Base*>( new Derived2 );
(Regretfully, at least one of the static_cast
is necessary.)
If you are dealing with a container of polymorphic objects, you'll need
shared_ptr
instead, and if you're returning a polymorphic object, or
otherwise passing it around, you will use unique_ptr
if you can
guarantee its availability, and auto_ptr
otherwise—in some cases
where you're passing it around, shared_ptr
might be more appropriate.
In the case of returning an object or passing it around, the cost of copying it might also be a motive for using a smart pointer, even if the object isn't polymorphic. In such cases, I'd still use value semantics (i.e. copying and assigning the object itself) unless I had a performance problem.
Note that smart pointers aren't only used for memory management. I
regularly use auto_ptr
in the interface of queues between threads:
once the object has been inserted into the queue, it no longer belongs
to the sending thread; auto_ptr
expresses these semantics exactly,
with the auto_ptr
in the sending thread becoming invalid. Or a
modifiable singleton (something which should be very, very rare) might
acquire a lock in its instance
function, and return a shared_ptr
which frees the lock in its final destructor. I've also used smart
pointers in one or two cases to ensure transactional semantics: in one
case, for example, the objects were in a number of different sets (which
held pointers to them, of course), ordered according to different
criteria. To modify an object, you acquired a shared pointer to it; the
function which returned this shared pointer also removed the objects
from the sets, so that you could safely modify the key values as well,
and the final destructor reinserted them into the sets according to the
new keys. And so on—there are any number of uses for smart
pointers that have nothing to do with memory management or object
lifetime.
Upvotes: 0
Reputation: 31435
new and delete do two things.
new
allocates memory and gets an object to construct itself in that memory space.
delete
first gets the object to destruct itself then releases the memory.
Note that some smart pointers can be given a custom deleter which doesn't call delete on the object but whatever you ask it to do. There may be occasions when you wish to pass it a no-op.
Upvotes: 0
Reputation: 11787
yes. that s what smart pointers are used for. http://www.boost.org/doc/libs/1_48_0/libs/smart_ptr/smart_ptr.htm
http://en.wikipedia.org/wiki/Smart_pointer
What is a smart pointer and when should I use one?
Upvotes: 1
Reputation: 258548
All class members are deleted even if you don't have a destructor when the instance is deleted. Memory leaks occur when you deal with pointers:
class A
{
private:
B* b;
};
In this case, b
itself will be destroyed when the instance of A
is deleted, but the memory it points to will not.
class A
{
private:
SmartPtr<B> b;
};
In the case of smart pointers, which usually have some reference counting and memory cleanup in the destructor, the memory it points to will be explicitly destroyed by its destructor, and the destructor of the smart pointer will be implicitly called when the instance of the containing class is destroyed.
Upvotes: 4
Reputation: 70931
The pointer to the object itself will be deleted. However if there is any dynamically allocated data in the class it will not get freed. The idea of having a destructor is to be able to post-process the class object and mainly - free any resources taken.
Upvotes: 0
Reputation: 73433
Yes, smart pointer deletes the object irrespective of whether class has destructor or not. Note that smart pointers are used with objects allocated on heap (using new
) and these object won't release memory when they go out of scope, you need to explicitly delete
them. Smart pointers will remove this process of explicitly deleting them.
Upvotes: 0