tags:

views:

220

answers:

4

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.

+1  A: 

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.

stevex
urllib is a class. And the exception occurs when the object is created.
Vicky
A: 

You might want to try inserting it if it doesnt exist in the map yet, although what you have should be fine

url_container.insert ( pair<int,urllib>(ucid,urllib()) );
SwDevMan81
I tried this way too. still it gives the same error
Vicky
Have you tried this: urllib* test = new urllib(); just to see if that works? If you decide to use the reference in the map, dont forget to delete it at the end
SwDevMan81
A: 

I guess the only reasonable way to solve access violations is to use a debugger.

Joonas Pulakka
A: 

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.

iain