EXC_BAD_ACCESS 当使用 std::function w/ std::bind 时

EXC_BAD_ACCESS when using std::function w/ std::bind

本文关键字:std function bind BAD ACCESS EXC      更新时间:2023-10-16
使用 std:

:function 和 std::bind 升级到 XCode 5 后,似乎产生了EXC_BAD_ACCESS异常。看起来 std::function 实现中的__base指针最终为 null,导致访问错误,但我不清楚为什么会这样。有没有人知道我做错了什么?

下面是说明该问题的示例代码。

struct A
{
    void foo(bool b)
    {
        std::cout << b << std::endl;
    }
    void go()
    {
        // ok
        auto a = std::bind(&A::foo, this, std::placeholders::_1);
        a(true);
        // ok
        std::function<void(A*, bool)> b = std::bind(&A::foo, std::placeholders::_1, std::placeholders::_2);
        b(this, true);
        // ok
        std::function<void(A*, bool)> c = std::bind(&A::foo, this, std::placeholders::_2);
    c(this, true);
        // EXC_BAD_ACCESS
        std::function<void(bool)> f = std::bind(&A::foo, this, std::placeholders::_1);
        f(true);
    }
};
...
...
A a;
a.go();

似乎这可能是一个已修复的错误。我有一个类似的问题(std::bind to std::function 崩溃与 Clang),解决方案只是从 XCode 5.0.1 升级到 XCode 5.0.2。

我尝试了您的代码,它似乎在 XCode 5.0.2 中运行良好(尚未尝试过 5.0.1)。