KeyboardKit icon indicating copy to clipboard operation
KeyboardKit copied to clipboard

KeyboardKit keyboards get randomly removed

Open danielsaidi opened this issue 4 years ago • 4 comments

There is a previous bug for this, but I decided to open a new one to keep it clean.

iOS sometimes (and randomly) removes KeyboardKit-based keyboards from the list of keyboards. You then have to go into System Settings > Keyboards to re-add it. It's very random and very hard to reproduce, and I see nothing obvious that makes this to happen. I first assumed that this was an iOS bug, just like how you sometimes have to restart an app in which you are typing, to show newly added keyboards.

However @ctawong mentioned in this post that this doesn't happen in 5.4.1, which if correct indicates that KeyboardKit does something between this version and 6.0 (this was already happening in 5.9) that makes iOS remove the keyboard.

With this new information, I was thinking that WHAT IF there is a memory leak that causes KeyboardKit to not properly deallocate keyboards once the used picks another keyboard. Could this make the amount of KeyboardKit processes too many, and cause iOS to fix the problem by removing the keyboard extension and force the user to re-add it?

I really need help with this, but will start looking for memory leaks to see if something turns up. If so, I would like to add better tools for inspecting memory leaks within the keyboard, since this is pretty tricky to debug.

danielsaidi avatar Apr 15 '22 05:04 danielsaidi

There seems to have a memory leak.

Memory leak

Reference count keeps on increasing when switching between keyboard kit and iOS default emoji page (or default iOS keyboard).

Steps to reproduce:

  • Debug keyboard scheme
  • Open keyboard in some field
  • Generate memory graph to see current reference count
  • Resume app
  • Switch between iOS default keyboard and keyboard kit couple of times
  • Generate memory graph to see increased reference count

Screenshot is of iPhone 14 simulator (reproducible in physical device too) Issue is reproducible in keyboard kit v.5.4.0 also.

nishinraj avatar Sep 19 '22 13:09 nishinraj

Thanks for finding this @nishinraj! These memory leaks are pretty nasty to resolve in the extension, but I'll see if I can find what's causing it. Any help is welcome.

danielsaidi avatar Sep 19 '22 14:09 danielsaidi

@nishinraj Have you found anything else regarding this? It's strange that the services are still used, but that the Keyboard view controller is deallocated.

danielsaidi avatar Sep 22 '22 19:09 danielsaidi

@nishinraj I have found the cause of the memory leak.

If I add this to KeyboardHostingController:

deinit {
    removeFromParent()
    view.removeFromSuperview()
}

I don't get any duplicate instances when switching between the demo keyboard and the native keyboards.

I will prepare a patch release with this fix. Would be great if you could confirm on your end.

danielsaidi avatar Sep 22 '22 20:09 danielsaidi