【问题标题】:EXC_BAD_ACCESS when using std::function w/ std::bind使用带有 std::bind 的 std::function 时的 EXC_BAD_ACCESS
【发布时间】:2013-11-06 18:40:55
【问题描述】:

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

这里是说明问题的示例代码。

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();

【问题讨论】:

  • 无法使用 gcc 4.7.2 和 clang 3.2 (Linux) 进行复制

标签: c++ xcode c++11 compiler-construction libstdc++


【解决方案1】:

看来这可能是一个已修复的错误。我有一个类似的问题 (std::bind to a std::function crashes with Clang),解决方案就是从 XCode 5.0.1 升级到 XCode 5.0.2。

我尝试了您的代码,它似乎可以在 XCode 5.0.2 上正常工作(没有尝试过 5.0.1)。

【讨论】:

    猜你喜欢
    • 2014-01-22
    • 2023-03-16
    • 1970-01-01
    • 2022-11-04
    • 1970-01-01
    • 1970-01-01
    • 2016-12-18
    • 2013-04-16
    • 2019-02-20
    相关资源
    最近更新 更多