简体   繁体   English

C ++类中的“ this” shared_pointer安全性

[英]C++ “this” shared_pointer safety in class method

I have a class derived from std::enable_shared_from_this. 我有一个派生自std :: enable_shared_from_this的类。 All class objects are managed by shared pointers, thus they get destructed automatically when there are no more shared pointers pointing to them. 所有类对象都由共享指针管理,因此当不再有指向它们的共享指针时,它们将自动销毁。

I have a class method which removes some shared pointers from the program's data structures, and there's a risk it removes all shared pointers to "this", ie the object on which the class method was called. 我有一个类方法,它从程序的数据结构中删除了一些共享的指针,并且存在着删除所有“ this”(即在其上调用了该类方法的对象)共享指针的风险。

The question is, if really all pointers are removed, is there a chance the object is destroyed while the method runs, and the "this" pointer simply becomes invalid? 问题是,如果真的删除了所有指针,在方法运行时是否有可能销毁对象,而“ this”指针只是变得无效? If I want to ensure it doesn't happen, can I trust the system or I have to create a shared_ptr to "this" inside the method, to keep the object alive until I'm done with it? 如果我想确保它不会发生,我可以信任系统吗?还是必须在方法内部创建一个shared_ptr来“ this”,以使对象保持活动状态直到完成操作? (And then if there's no more pointers, it's okay to have it destructed, once the method execution ends) (然后,如果没有更多的指针,则可以在方法执行结束后对其进行销毁)

EXAMPLE: 例:

class SharedObj : public std::enable_shared_from_this<SharedObj>
{
   /* ... */
    void do_something(SharedObj& a, SharedObj& b);
    std::shared_ptr<SharedObj> child;
};

void SharedObj::do_something(SharedObj& a, SharedObj &b)
{
   /* ... */
   a.remove_child();
   b.remove_child();
}

If a and b are the only ones who have a shared_ptr pointing to "this", then after the two remove_child() rows, there are no shared pointers pointing to "this", so besically it's supposed to be automatically destructed 如果a和b是仅有的shared_ptr指向“ this”的那些,那么在两行remove_child()行之后,没有共享的指针指向“ this”,因此一般来说,它应该被自动销毁

You certainly can wind up destructing your instance from within a method by doing what you're doing. 当然,您可以通过执行操作来从方法内部破坏实例。 But this situation isn't specific to enable_shared_from_this , you can always call something in a method that can result in your own class's destruction. 但是这种情况并非特定于enable_shared_from_this ,您始终可以在可能导致您自己的类被破坏的方法中调用某些内容。 This is really only an error if you try to deference this after your class is destroyed. 如果您尝试在销毁类之后遵从this ,则这实际上只是一个错误 If you go on with regular code that doesn't modify or access the class instance, you are safe. 如果继续使用不会修改或访问类实例的常规代码,那么您是安全的。

What you suggested (to hold a shared_ptr to your own instance during do_something 's execution) is a good idea, if you need the instance to persist for the entire function execution. 如果您需要实例在整个函数执行过程中保持不变,那么建议的做法(在do_something执行期间将shared_ptr保留在自己的实例中)是一个好主意。

As an alternative or in addition to Dave's answer if you are using shared pointers throughout for managing lifetimes then you should only ever be calling the object through a shared pointer anyway so that pointer will be keeping the object alive until after the function returns. 如果您在整个生命周期中都使用共享指针,则作为Dave的替代方法或补充,您无论如何应该只通过共享指针调用对象,这样指针才能使对象保持活动状态,直到函数返回为止。 If you store a non-owning pointer to a shared pointer managed object then you should be storing a weak pointer and locking it before using it. 如果存储指向共享指针管理对象的非所有者指针,则应该存储一个弱指针并在使用它之前将其锁定。 That shared pointer created by locking will again keep the object alive. 通过锁定创建的共享指针将再次使对象保持活动状态。

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

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