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

女生的网名这么多〃 提交于 2019-12-07 16:56:04

问题


After upgrading to XCode 5 using std::function with std::bind appears to be generating EXC_BAD_ACCESS exceptions. It looks as if the __base pointer inside the implementation of std::function ends up being null, resulting in the bad access, but I'm not clear on why that would be the case. Does anyone have an insight into either what I'm doing wrong?

Here is sample code that illustrates the problem.

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

回答1:


It seems that this may be a bug that has been fixed. I had a similar issue (std::bind to a std::function crashes with Clang) where the solution simply was to upgrade from XCode 5.0.1 to XCode 5.0.2.

I tried your code and it seems to work fine with XCode 5.0.2 (have not tried with 5.0.1).



来源:https://stackoverflow.com/questions/19820128/exc-bad-access-when-using-stdfunction-w-stdbind

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!