我该如何追踪C++11 lambda中对象的生命周期?

7

有时候,我们对于捕获对象状态的 lambda 表达式的生命周期一无所知(例如从对象返回它、将其注册为回调函数但无法取消订阅等)。如何确保在调用时 lambda 不会访问已经被销毁的对象?

#include <iostream>
#include <memory>
#include <string>

class Foo {
public:
    Foo(const std::string& i_name) : name(i_name) {}

    std::function<void()> GetPrinter() {
        return [this]() {
            std::cout << name << std::endl;
        };
    }

    std::string name;
};

int main() {
    std::function<void()> f;

    {
        auto foo = std::make_shared<Foo>("OK");
        f = foo->GetPrinter();
    }

    auto foo = std::make_shared<Foo>("WRONG");

    f();

    return 0;
}

这个程序打印出"WRONG"而不是"OK" (http://ideone.com/Srp7RC),这只是巧合(似乎它只是为第二个Foo对象重用了相同的内存)。无论如何,这是一个错误的程序。当我们执行f时,第一个Foo对象已经消失了。

1
代码未被clang接受。 - P0W
您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - Stas
是的,它在最新版本上可以编译,谢谢! - P0W
2个回答

14

延长对象生命周期

Lambda函数可以捕获指向this的共享指针,因此只要存在至少一个Lambda函数,对象就不会被销毁。

class Foo : public std::enable_shared_from_this<Foo> {
public:
    Foo(const std::string& i_name) : name(i_name) {}

    std::function<void()> GetPrinter() {
        std::shared_ptr<Foo> that = shared_from_this();

        return [that]() {
            std::cout << that->name << std::endl;
        };
    }

    std::string name;
};

http://ideone.com/Ucm2p8

通常情况下,这不是一个好的解决方案,因为对象的生命周期在这里以非常隐式的方式被延长。这是产生对象之间循环引用的非常简单的方法。
跟踪对象的生命周期
Lambda表达式可以跟踪捕获对象的生命周期,并仅在该对象仍然存在时使用它。
class Foo : public std::enable_shared_from_this<Foo> {
public:
    Foo(const std::string& i_name) : name(i_name) {}

    std::function<void()> GetPrinter() {
        std::weak_ptr<Foo> weak_this = shared_from_this();

        return [weak_this]() {
            auto that = weak_this.lock();
            if (!that) {
                std::cout << "The object is already dead" << std::endl;
                return;
            }

            std::cout << that->name << std::endl;
        };
    }

    std::string name;
};

http://ideone.com/Wi6O11

不使用shared_ptr跟踪对象生命周期

正如hvd 所指出的,我们并不能总是确定一个对象是否由shared_ptr管理。在这种情况下,我建议使用以下的lifetime_tracker。它是自包含的,不会影响您管理对象生命周期的方式。

struct lifetime_tracker
{
private:
    struct shared_state
    {
        std::uint32_t count : 31;
        std::uint32_t dead  : 1;
    };

public:
    struct monitor
    {
        monitor() : state(nullptr) {}

        monitor(shared_state *i_state) : state(i_state) {
            if (state)
                ++state->count;
        }

        monitor(const monitor& t) : state(t.state) {
            if (state)
                ++state->count;
        }

        monitor& operator=(monitor t) {
            std::swap(state, t.state);
            return *this;
        }

        ~monitor() {
            if (state) {
                --state->count;
                if (state->count == 0 && state->dead)
                    delete state;
            }
        }

        bool alive() const {
            return state && !state->dead;
        }

    private:
        shared_state *state;
    };

public:
    lifetime_tracker() : state(new shared_state()) {}
    lifetime_tracker(const lifetime_tracker&) : state(new shared_state()) {}
    lifetime_tracker& operator=(const lifetime_tracker& t) { return *this; }

    ~lifetime_tracker() {
        if (state->count == 0)
            delete state;
        else
            state->dead = 1;
    }

    monitor get_monitor() const {
        return monitor(state);
    }

private:
    shared_state *state;
};

使用示例

class Foo {
public:
    Foo(const std::string& i_name) : name(i_name) {}

    std::function<void()> GetPrinter() {
        auto monitor = tracker.get_monitor();

        return [this, monitor]() {
            if (!monitor.alive()) {
                std::cout << "The object is already dead" << std::endl;
                return;
            }

            std::cout << this->name << std::endl;
        };
    }

private:
    lifetime_tracker tracker;

    std::string name;
};

5

当你可以确定对象由shared_ptr管理时,Stas的答案是很好的,但这并不总是可能的。然而,你总是可以跟踪对象的生命周期,并在lambda中添加断言。

void ignore(void *) { }

class Foo {
public:
    Foo(const std::string& i_name) : name(i_name) {}
    Foo(const Foo& other) : name(other.name) {}
    Foo(Foo&& other) : name(std::move(other.name)) {}
    Foo& operator=(Foo other) { swap(*this, other); return *this; }
    friend void swap(Foo& a, Foo& b) { using std::swap; swap(a.name, b.name); }

    std::function<void()> GetPrinter() {
        std::weak_ptr<void> monitor = this->monitor;

        return [=]() {
            assert (!monitor.expired());
            std::cout << name << std::endl;
        };
    }

    std::string name;

private:
    std::shared_ptr<void> monitor{this, ignore};
};

在构造函数中,共享指针monitor没有被明确初始化,但是通过其初始化器设置为指向this,并在指针的生命周期过期时不执行任何操作。这样做的想法不是让shared_ptr负责释放对象,而是让shared_ptr传递有关对象生命周期的信息。
在创建lambda之前,您可以构造一个跟踪关联shared_ptrweak_ptr。如果对象已被销毁,则其monitor成员也必然已被销毁,并且可以通过expired()函数看到这一点。

1
是的,好主意。但你需要定义复制构造函数。现在,如果你复制Foo对象,它会表现不正确。 - Stas
@Stas 没错,谢谢你指出来。还有移动构造函数和赋值运算符。 - user743382

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接