简体   繁体   English

关于shared_ptr

[英]about shared_ptr

Just want to clarify regarding shared_ptr 只是想澄清有关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? 在上面的代码中,如果未删除或释放objb ,是否存在内存泄漏? In the end, obja will go out of scope and will free itself. 最后, obja将超出范围并释放自身。 Since objb and obja are pointing in the same instance does that mean there's no need in freeing objb ? 由于objbobja指向同一个实例,这是否意味着不需要释放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. 原始指针没有任何所有权语义,从shared_ptr get原始指针也不会增加引用计数。

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? 在上面的代码中,如果未删除或释放objb,是否存在内存泄漏?

No. The Foo object is owned by the obja shared pointer. Foo对象由拥有obja共享指针。 By doing .get() , you retrieve a "dumb", observing pointer that does not have any influence on the lifetime of the pointed object. 通过执行.get() ,您将检索“哑”,观察指针,该指针对指向对象的生存期没有任何影响。 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? 由于objb和obja指向同一个实例,这是否意味着不需要释放objb?

There is no need of freing objb because when obja goes out of scope, its destructor will delete the owned object. 不需要刷新objb因为当obja超出范围时,其析构函数将delete拥有的对象。 If you did delete objb , you would get Undefined Behavior by trying to delete an object twice. 如果确实delete objb ,则尝试两次删除一个对象将获得未定义行为。

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. obja仍然拥有该内存,并且在范围出口处销毁objaobja其释放。 Getting the pointer isn't equivalent to releasing its ownership (qv release() ). 获取指针并不等同于释放其所有权(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 . 因此,您会通过get()得到一个弱的(非拥有的)指针,只要实际的拥有者存在,该指针就有效,但是如果原始指针的寿命超过了所拥有的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. 因此,C和C ++中的指针只是内存的地址。 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. 您可以将指针看作数字,当您将一个指针分配给另一个指针时,您只需复制该数字,该地址的内存就不会发生任何变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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