iOS Keyboard Extension Memory Crash

2020-07-24 15:23发布

问题:

I've faced a memory problem with iOS keyboard extension. I have a custom view for keyboard view. I initialized it in loadView() function of UIInputViewController. Then add it to the self.view as a child. But I have a memory problem especially in WhatsApp app. Keyboard extension crashes after 4-5 times in a row hide/show keyboard operation. I launched the memory allocation graphic from Instruments app. And I saw that the size of memory usage is increasing every "show" keyboard operation and exceed the memory limit (I think its 30-40 MB). And there were too many my custom keyboard view object allocations.


For simplify the problem :

I've created a new Xcode project and I added a new Custom Keyboard Extension Target by using Editor > Add Target > Custom Keyboard Extension.

Then, I ran that default templated keyboard on my real device (not happened in simulator). And I opened Instruments > Allocations application to profile memory allocations of extension.

When I was trying the keyboard on WhatsApp, I've noticed that number of Persistent ViewController objects were increased while hide/unhide the keyboard by tap on the outside of the keyboard and tap input area.

Number of Persistent KeyboardViewController:

2 -- hide -- > 1 -- show -- > 3 -- hide --> 2 -- show --> 4 (increasing)

I was trying the keyboard on iOS Messages app too. And there was no duplication.

1 -- hide --> 0 -- show --> 1 -- hide --> 0 ( its ok )

Do u have any idea about this issue ? Why WhatsApp application duplicates the keyboard extension viewController.