Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Read access violation on lambda inside lambda written to unorderd_map

I am getting a read access violation in this code:

    void attach(keyType eventName, funcType &&func) noexcept
    {
        m_events[eventName] = std::move(func);
    }

https://github.com/AntonioCS/AcsGE/blob/master/Util/Mediator.h#L38

This normally works when I just send a lambda but with this code it seems to fail:

    auto outterFunc = [innerFunc = std::move(func)](SDL_Event &event)
    {
        int x{ 0 };
        int y{ 0 };
        SDL_GetMouseState(&x, &y);

        innerFunc(x, y);
    };

    attach(SDL_MOUSEMOTION, std::move(outterFunc));
}

https://github.com/AntonioCS/AcsGE/blob/master/EventManager.cpp

This is called with this:

getEventManager()->onMouseMove([](int x, int y)
{
    std::cout << "MOUSE X: " << x << " MOUSE Y: " << y << '\n';
});

https://github.com/AntonioCS/tictactoecpp2/blob/master/tictactoe/StartMenuState.cpp

Note that on onMouseMove I receive a lambda and then I wrap it in another lambda and send that to the attach method.

I believe this is the root cause.

What am I doing wrong here?

NOTE: A pointer was not being set. MSVC, in the debugger, still navigated the code as if it was being called correctly but the pointer was not set

Comments