Error at runtime

I'm getting this error when I press a key or even when I press the Up key which I assigned to the map.

1
2
Exception thrown: read access violation.
_Scary->_Myhead was 0xFFFFFFFFFFFFFFF7.


on the line: keyPressedMap[event.key.code](this);

inside of my class:
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
27
28
29
30
class kinematicObject: public Object {
public :

	kinematicObject():Object() { }
	kinematicObject(sf::Texture t):Object { t } { }

	void operator()(sf::Event event) {
		if (event.type == sf::Event::KeyPressed) {						
			keyPressedMap[event.key.code](this);
			
		}
		else if (event.type == sf::Event::KeyReleased) {
			keyReleasedMap[event.key.code](this);
		}
	}

	void pushKeyPressedAndAction(sf::Keyboard::Key key, std::function<void(kinematicObject*)> f) {
		keyPressedMap.insert({ key, f });
	}

	void pushKeyReleasedAndAction(sf::Keyboard::Key key, std::function<void(kinematicObject*)> f) {
		keyReleasedMap.insert({ key, f });
	}

private:

	std::map<sf::Keyboard::Key, std::function<void(kinematicObject*)>> keyPressedMap;
	std::map<sf::Keyboard::Key, std::function<void(kinematicObject*)>> keyReleasedMap;

};


this is how I play this function call:
1
2
3
4
5
6
7
8
9
10
11
12
13
void gameWindow::logic() {
	sf::Event event;
	while (window.pollEvent(event)) {
		if (event.type == sf::Event::Closed)
			window.close();

		for (int i = 0; i < kinematicObjectStack.size(); ++i) {
			kinematicObject* k = static_cast<kinematicObject*>(&kinematicObjectStack[i]);
			(*k)(event);
		}

	}
}

(the cast is because this kinematicObjectStack member is of type std::vector<Object> which is a base of kinematicObject)

and this is what I put inside the map:
1
2
3
4
5
	player.pushKeyPressedAndAction(sf::Keyboard::Up, 
		[](kinematicObject* k) {
			k->getSprite().move(sf::Vector2f{ 0,-32 }); 
		}
	);


I probably doing something wrong with std::map..

any help would be appreciated, also I thought map is the wrong way doing what I want but I want to make it work for the sake of exercise...
Last edited on
none of the code posted refers to a myhead which is the piece that has a wonky pointer.
Yes, I don't know how I got there, probably something related to the accessing the map does that jump. , VS when error open a new page named xtree, there it points to a line: _Tree_find_result<_Nodeptr> _Result{{_Scary->_Myhead->_Parent, _Tree_child::_Right}, _Scary->_Myhead};

and a popup with an error of access violation etc...

1
2
3
4
5
6
7
8
9
10
	kinematicObject player;
	sf::Event event;
	event.key.code = sf::Keyboard::Up;
	std::map<sf::Keyboard::Key, std::function<void(kinematicObject*)>> m;
	m.insert({ event.key.code, [](kinematicObject* k) { 		
		k->getSprite().move(sf::Vector2f{ 0,-32 });
		std::cout << "success";
		} 
	});
	m[event.key.code](&player);


this works, I just tried to do it on a small scale and it succeeded, so still I don't know what's the problem with the bigger example...

I think the static_cast don't work as expected.. let me investigate...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void gameWindow::addObject(Object* obj) {
	 Object* o = new Object{ *obj };
	switch (o->getType()) {
	case Object::TYPE::BACKGROUND_OBJ: {
		bgObjectStack.push_back(*o);
		break;
	}
	case Object::TYPE::KINEMATIC_OBJ: {
		kinematicObjectStack.push_back(*o);
		break;
	}
	case Object::TYPE::STATIC_OBJ: {
		staticObjectStack.push_back(*o);
		break;
	}
	case Object::TYPE::MOVABLE_OBJ: {
		movableObjectStack.push_back(*o);
		break;
	}
	default:break;
	}
}

I think this is the cause of the problem, I'm creating with new, Object object, and give him a kinematicObject, and then he slicing it, and the cast don't work..
I think that's the problem...
Last edited on
OK, thank for every one, problem solved I was doing some really bad thing with that cast and that's the cause, I tweaked the code a little to
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
void gameWindow::addObject(Object* obj) {
	Object* o = new Object{ *obj };
	switch (o->getType()) {
	case Object::TYPE::BACKGROUND_OBJ: {
		bgObjectStack.push_back(*o);
		break;
	}
	case Object::TYPE::KINEMATIC_OBJ: {
		kinematicObject* ko = new kinematicObject{};
		ko = static_cast<kinematicObject*>(obj);
		if(ko)
			kinematicObjectStack.push_back(*ko);
		break;
	}
	case Object::TYPE::STATIC_OBJ: {
		staticObjectStack.push_back(*o);
		break;
	}
	case Object::TYPE::MOVABLE_OBJ: {
		movableObjectStack.push_back(*o);
		break;
	}
	default:break;
	}
}


and changed the type of the kinematicObjectStack to std::vector<kinematicObject>
1
2
3
4
5
6
7
8
9
10
11
12
void gameWindow::logic() {
	sf::Event event;
	while (window.pollEvent(event)) {
		if (event.type == sf::Event::Closed)
			window.close();

		for (int i = 0; i < kinematicObjectStack.size(); ++i) {
			kinematicObjectStack[i](event);
		}

	}
}


and every thing just working fine :)
Topic archived. No new replies allowed.