Access Violation Error

Jun 15, 2010 at 12:33am
I am trying to use a map again to manage objects.

Each object gets a unique "name" (actually called ID) and then a pointer to the object is stored in a map of strings (The IDs or Keys) and then Object* But when I try to do this in the constructor of an object:
ObjectMan.insert(pair<string, Object*>(ID, this));
I get an access violation error. I have tried numerous things to resolve this, but I don't know what the problem is. The debug stops in the xtree file, but I know it's the line described above that is causing the access violation.

I'm not quite good at using pointers, but I understand the basics of them. I would like to know how I would go upon storing pointers to objects in a map of pointers in the objects constructor. I have no idea why the access violation is happening, might just need to change a "*" or "&" symbol here or there.

Thanks for your help
Last edited on Jun 15, 2010 at 12:37am
Jun 15, 2010 at 12:38am
Something you probably need to know is that, the Map is a container for an Object class, but the class I'm trying to store is an inherited class from the Base Object Class.

Also, the ObjectManager class is not a map itself, but a class based off of a map
1
2
3
4
5
6
struct ObjectManager : map<string, Object*>
{
	//Functions
	void renameEntry(string ID, string newID); //Renames the string associated with an object
	void removeEntry(string ID); //Removes an entry of an object
};
Last edited on Jun 15, 2010 at 1:00am
Jun 15, 2010 at 9:36am
i think you should try to store pair in some other function & not in constructor.

(well i am not sure still try this).
Jun 15, 2010 at 2:02pm
That didn't work. I think it's because the object I'm trying to store is too big or something for the pointers map
Jun 15, 2010 at 2:13pm
I think I'm going to have to make the Object manager store like a tree type thing.

Where the Whole manager stores maps of different object types
Jun 15, 2010 at 3:50pm
It still gets access violation errors. I think it's because "this" is a constant and somewhere in the magical windows files it tries to edit the pointer that I pass to the map.

All I want is a map that stores pointers to Objects.
Jun 15, 2010 at 4:11pm
I'm not sure what's causing your problem, but don't inherit from STL containers. They don't have virtual destructors and it's much easier and clearer to treat them as black box abstractions.

Also, you should avoid doing anything in a constructor that might throw an exception, because that will leave your object in such a state that the destructor will not be called.
Jun 16, 2010 at 4:25am
try for friend function .

function(map<string,obj* >& some_map , string s ,object * some_obj) // friend
{
some_map.insert(make_pair(s,some_obj));
}

( well i am not sure about syntax try above)
Jun 16, 2010 at 4:47am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include<iostream>
#include<map>
#include<string>
using namespace std;

class myc
{
  public:
  friend void myc_st(map<string , myc* >& m , string s , myc * ptr );
};

void myc_st(map<string , myc* >& m , string s , myc * ptr )
{
  m.insert(make_pair(s,ptr));
}

int main()
{
  map<string , myc* > m;
  
  myc  c;
  myc * ptr = &c;
 
  myc_st(m,"vivmen",ptr);
  return 0;
}


try something like this, it will store pointer to object in map(i think so ..).
Topic archived. No new replies allowed.