从对象成员函数内部延迟删除操作的技术/设计模式?

Techniques / design patterns for postponed delete action from inside of object member function?

假设我遇到了这样一种情况,我知道我想删除一个对象——当我正在执行所述对象的成员函数的代码部分时。换句话说,在函数将 return 编辑成 return 后,我希望对象被破坏。是否存在适用于这种情况的技术或设计模式?我想尝试从任何对象内部调用析构函数是不安全的(甚至是不允许的?)

也欢迎回答解释为什么这是一个坏主意以及如何做。

你描述的是Resource Acquisition is Initialization(简称RAII)的全部基础。简而言之,处理程序对象将持有并拥有您分配的内存,而持有的内存与持有者的生命周期相关。也就是说当holder对象消失的时候,它所承载的资源也被妥善销毁了。

这方面的一个例子如下:

class Class { /* definition */ };

int doOperation(/* arguments */) {

  // code

  // this 'smart pointer' contains an object of type Class
  // you create an object of type Class via dynamic allocation and then it is stored within the ptr object
  // this will hold the memory until the end of the function
  std::unique_ptr<Class> ptr = std::make_unique<Class>(/*arguments to pass to the object*/);

  // use ptr
  // assign a return value to returnValue

  return returnValue;
  // as the function ends, the object ptr is automatically destroyed that in turn will 
  // automatically delete the memory of object Class it held
}

std::unique_ptr 的使用是 RAII 模式的一个例子。其他智能指针,如 std::shared_ptr,也实现了这种模式。

我想你想要一个自包含的对象。

这可以使用 "holds" 本身具有强引用的对象来实现(C++ 中的强引用称为 shared_ptr,它是 smart pointers.

#include <iostream>
#include <chrono>
#include <memory>
#include <thread>

using namespace std;

class LengthyOperation {
private:
    // Just a marker, for debugging, to differentiated between objects, and to indicate
    // a released object if illogical value (or if run under Valgrind / AddressSanitizer)
    int i;

    // Privatise the constructor, so it can't be constructed without the static factory method.
    LengthyOperation(): i(0) {}
    LengthyOperation(int i): i(i) {}

    // The "Holder", a reference to "this".
    weak_ptr<LengthyOperation> holder;

public:

    int getId() {
        return i;
    }

    void executeTheOperation() {
        // Strongify the weak "holder" reference
        // So that no-one would release the object without ending of this function
        shared_ptr<LengthyOperation> strongHolder = holder.lock();


        // Simulate a "lengthy" operation, by pausing this thread for 1 second
        std::this_thread::sleep_for(std::chrono::seconds(1));

        cout << "Operation " << i << " ends" << "\n";

        // Remove the reference to "this" in the holder.
        holder.reset();

        // Now, the "strong" reference which was temporary created (strongHolder)
        // is removed when the scope ends. So that if it is held somewhere
        // else, it will not be released until all other holders release it.
        // Make sure you will NOT need it again here, because the object
        // may be released from memory.
    }

    ~LengthyOperation() {
        cout << "Object with id: " << i << " Will destruct now" << "\n";
    }

    static shared_ptr<LengthyOperation> factory(int i = 0) {
        shared_ptr<LengthyOperation> ret = shared_ptr<LengthyOperation>(new LengthyOperation(i));
        // Make the weak pointer "holder", hold a reference to "this"
        ret->holder = ret;
        return ret;
    }
};

int main() {
    thread thr1([](){
        weak_ptr<LengthyOperation> operation1Weak;
        {
            shared_ptr<LengthyOperation> operation1 = LengthyOperation::factory(3);
            operation1Weak = operation1;
            operation1->executeTheOperation();
            cout << "Still there is a strong reference: it refers to object with id "
                    << operation1->getId() << "\n";
            cout << "Releasing the strong reference" << "\n";
        }
        cout << "No strong reference: it is "
                << (operation1Weak.expired() ? "invalid" : "valid") << "\n";

    });

    // Wait for a relative long time, to give chance for all threads to end
    // One could use "join" as a better approach.
    std::this_thread::sleep_for(std::chrono::seconds(2));

    // Detach the thread to avoid crashes
    thr1.detach();


    thread thr2([](){
        // Make an operation, an execute it directly without putting any strong reference to
        LengthyOperation::factory(5)->executeTheOperation();
    });

    std::this_thread::sleep_for(std::chrono::seconds(2));

    thr2.detach();

    thread thr3([](){
        // Try to create the object, without executing the operation, to see what
        // weakening the "holder" pointer have done.
        weak_ptr<LengthyOperation> oper = LengthyOperation::factory(1);
        cout << "The weak non-called is " << (oper.expired() ? "expired" : "valid") << "\n";
    });

    std::this_thread::sleep_for(std::chrono::seconds(1));

    thr3.detach();

    return 0;
}

这就像在 executeTheOperation 中调用 "delete",但更安全一些,因为它确保没有其他对象需要它。

同样使用 RAII 更好,但这把责任推到了 "caller" 的手上。谁实例化了对象,必须释放它。

(这个答案在评论说如果你不调用 executeTheOperation 强 "holder" 引用会导致内存泄漏,应该将他的代码设计为自我纠正如果它的用户不能正确调用它)