简体   繁体   中英

access violation error when using map in dll

I tried to create a win32 dll using c++. It has a map declared globally. But when I try to access the map using the dll its giving a run time error that: WindowsError: exception: access violation reading 0x00000008 . How to solve it?

Declaration: static map<int,urllib> url_container;

The urllib is a class.

Error occurance: url_container[ucid] = urllib();

The error occurs at the above point.

I assume urllib is a type or class and not a function?

It doesn't look like there's anything wrong with your code. In the debugger, what do you see on the call stack when the exception happens? It would be helpful to see exactly where it's running into the access violation.

如果地图中尚不存在它,您可能想尝试将其插入,尽管您应该拥有的还不错

url_container.insert ( pair<int,urllib>(ucid,urllib()) );

我猜想解决访问冲突的唯一合理方法是使用调试器。

Does this code

url_container[ucid] = urllib()

get called in a static initialiser for an other global object? If so there is no guarantee that url_container has been consutructed before the other global object.

Use an accessor function to control when the object is created, or use a singleton library like boost singleton

Accessor example

map<int,urllib> & get_url_container()
{
    static map<int,urllib> url_container;
    return url_container
}

As an aside I would suggest you try to avoid global objects. As you could spend the rest of your life debugging issues like this. Eventually the construction of one global object will depend on another etc. and the order of construction is not defined so it might work on one platform/compiler and fail on another.

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