tags:

views:

61

answers:

2

So what happens to a pointer if you release an object owned by auto_ptr but do not actually assign it to a raw pointer? It seems like it's supposed to be deleted but it never gets the chance to. So does it get leaked out "into the wild"?

void usingPointer(int* p); 

std::auto_ptr<int> point(new int);
*point = 3;

usingPointer(point.release());

Note: I don't use auto_ptr anymore, I use tr1::shared_ptr now. This situation just got me curious.

+2  A: 

release isn't suppose to delete the owned point, from the docs:

Sets the auto_ptr internal pointer to null pointer (which indicates it points to no object) without destructing the object currently pointed by the auto_ptr.

Also, it's overkill to replace all uses of your auto_ptr with tr1::shared_ptr - you should be using unique_ptr where a shared one isn't necessary.

Idan K
I mostly replaced it when I realized auto_ptr doesn't apply a const copy constructor, and I would need the pointer to be copied and used among different instances of a class.
JustChris
+3  A: 

Unless usingPointer is calling delete on p, this is a memory leak. If you would call get instead of release then the memory will be automatically deleted when point falls out of scope.

dalle