简体   繁体   中英

about shared_ptr

Just want to clarify regarding shared_ptr

int main(){
   typedef std::tr1::shared_ptr<Foo> _foo;

   _foo obja(new Foo());
   Foo *objb = obja.get(); 

   // delete objb; //deleting objb will throw double free or corruption
   return 0;
}

In the code above, is there a memory leak if objb is not deleted or freed? In the end, obja will go out of scope and will free itself. Since objb and obja are pointing in the same instance does that mean there's no need in freeing objb ?

Is the above the same as this:

Foo *obja = new Foo();
Foo *objb;

objb = obja;
delete obja;

No, there is no leak. Raw pointers do not have any ownership semantics, and neither does get ting a raw pointer from a shared_ptr increase the reference count.

When the shared pointer goes out of scope in your example, the object pointed to is destroyed. As you have noticed already, manually deleting it consequently leads to undefined behaviour.

In the code above, is there a memory leak if objb is not deleted or freed?

No. The Foo object is owned by the obja shared pointer. By doing .get() , you retrieve a "dumb", observing pointer that does not have any influence on the lifetime of the pointed object. You just have to be careful not to dereference it after the pointed object has ceased to exist, or you will get Undefined Behavior.

Since objb and obja are pointing in the same instance does that mean there's no need in freeing objb?

There is no need of freing objb because when obja goes out of scope, its destructor will delete the owned object. If you did delete objb , you would get Undefined Behavior by trying to delete an object twice.

Is the above the same as this:

I guess one could say so, yes.

Nope, no leak. obja still owns the memory, and it is released when obja is destroyed at scope exit. Getting the pointer isn't equivalent to releasing its ownership (qv release() ). So you're getting a weak (non-owning) pointer via get() , which is valid as long as the actual owner exists, but is a dangling pointer if the raw pointer outlives the owning shared_ptr .

I think most answers above shows they do not understand where your confusion is.
So pointer in C and C++ is just an address of memory. When you "copy" one raw pointer to another you make them both point to the same address:

int *ptr1 = new int(); // points to memory at address lets say 1234;
int *ptr2 = ptr1; // now also points to 1234
*ptr1 = 10; // modify int where ptr1 points to
std::cout << *ptr2 << std""endl; // print 10 as ptr2 points to the same place as ptr1
delete ptr1; // I do not need memory at address 1234 anymore
delete ptr2; // I do not need memory at address 1234 again, error double deletion

You can look at pointers as numbers, when you assign one pointer to another you just copy that number, nothing happens to the memory at that address.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM