I'm using sfml 2.0 for developing my game. Game development is almost finished and I've been looking for some bugs and fixing them these days.
I've got SEGFAULT from sf:Keyboard::isKeyPressed. Typically, in normal case, keyboards inputs are well processed and exactly recognize what key is pressed. But after turn off and on my keyboard(apple wireless keyboard), my game is crashed.
bool HIDInputManager::isKeyPressed(Keyboard::Key key)
{
if (!m_isValid) {
sf::err() << "HIDInputManager is invalid." << std::endl;
return false;
}
// state = true if at least one corresponding HID key is pressed
bool state = false;
for (IOHIDElements::iterator it = m_keys[key].begin(); it != m_keys[key].end(); ++it) {
IOHIDValueRef value = 0;
IOHIDDeviceRef device = IOHIDElementGetDevice(*it);
IOHIDDeviceGetValue(device, *it, &value);
if (!value) {
// This means some kind of error / deconnection so we remove this
// element from our keys
CFRelease(*it);
it = m_keys[key].erase(it);
} else if (IOHIDValueGetIntegerValue(value) == 1) {
// This means the key is pressed
state = true;
break; // Stop here
} else {
// This means the key is released
}
}
return state;
}
The game was stopped at "IOHIDDeviceRef device = IOHIDElementGetDevice(*it);" because "*it" was null.
I think this problem is caused by releasing elements in m_keys[] due to reconnection of keyboard or something else.
To identify what key is pressed, "HIDInputManager::isKeyPressed(Keyboard::Key key)" function is called several times. At every function calls, "value", returned from "IOHIDDeviceGetValue()" might be null and function releases "m_keys[]" as shown above code.
when the number of vector(m_keys[]) becomes zero, game crashes because there is no elements in the vector.
I need an advice to fix it.