简体   繁体   中英

c++ Access violation when accessing map

I have a map<string, std::function<void(AgentMessage&)>> (AgentMessage is a struct with a few strings). When I try to access it using an iterator I get an access violation on the copy function of pair .

note: The std::function is pointing at a function in a different dll than the place where it is copied.

EDIT: I thought the explanation was good enough for a simple piece of code, but still - here it is.

for (map<string, std::function<void(AgentMessage&)>>::iterator it = mapRef.begin(); it != mapRef.end(); it++)
{
    auto functionCopy = it->second; // IT CRASHES HERE
}

Can you show the code that inserts elements to the map?

I tried this and it works:

#include <functional>
#include <map>
#include <string>

using namespace std;

struct AgentMessage
{

};

void f(AgentMessage& am)
{

}

void g(AgentMessage& am)
{

}

int main()
{
    AgentMessage am;
    map<string, std::function<void(AgentMessage&)>> m;

    m["f"] = f;
    m["g"] = g;

    for (map<string, std::function<void(AgentMessage&)>>::iterator it = m.begin(); it != m.end(); ++it)
    {
        auto func = it->second;
        func(am);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM