Fixed crash related to weak references

Technically, there are some valid situation where the keyboardActiveInput could be released before it is removed.