Fix fake mouse occasionally crash #151
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The issue
After the keymap loads, if the first fake mouse event is a key release event (instead of a key pressed event), the game would crash.
How to reproduce
Open any game. Switch to another window. Press left mouse button and while holding it, switch to the game window with cmd+tab. Release left mouse button inside game window, the game will crash.
To trigger the issue, mouse can't have any clicks in the game window before that release action. Or you can open keymap editor then close it, that would re-load the keymap and "clear" previous clicks.
Cause
The mouse position is obtained in mouse pressed event and mouse moved event. And the position variable was force unwrapped. So if a released event comes before any pressed event or moved event (actually no moved event would reach there if there were no pressed event, as the event handler for the moved event is only registered during handling of the pressed event), handler of released event would be force unwrapping a
nil
value.Fix
Don't make it an optional, and give it an initial pos during class initialization. That would lead to an extra touch ended event be sent, but that event would eventually be ignored for it has empty point ID.