简体   繁体   English

在DLL中使用地图时出现访问冲突错误

[英]access violation error when using map in dll

I tried to create a win32 dll using c++. 我试图使用c ++创建一个win32 dll。 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 . 但是,当我尝试使用dll访问地图时,给出了运行时错误: WindowsError:异常:访问冲突读取0x00000008 How to solve it? 怎么解决呢?

Declaration: static map<int,urllib> url_container; 声明: static map<int,urllib> url_container;

The urllib is a class. urllib是一个类。

Error occurance: url_container[ucid] = urllib(); 错误发生: url_container[ucid] = urllib();

The error occurs at the above point. 错误发生在以上几点。

I assume urllib is a type or class and not a function? 我假设urllib是类型或类而不是函数?

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. 如果是这样,没有这样的保证url_container已经在全球其他对象之前被consutructed。

Use an accessor function to control when the object is created, or use a singleton library like boost singleton 使用访问器函数控制何时创建对象,或使用单例库(如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. 最终,一个全局对象的构造将取决于另一个,等等,并且构造顺序未定义,因此它可能在一个平台/编译器上工作而在另一个平台/编译器上失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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