I'm a bit lost on the memory management in KMM obj...
# kotlin-native
m
I'm a bit lost on the memory management in KMM obj-c. I have
AvAudioPlayer
which uses a delegate
AVAudioPlayerDelegateProtocol
. Now I wanted to just use an anonymous implementation of that protocol (or a class extending it - they both work the same) and set it more or less like this
Copy code
player.delegate = object : NSObject(), AVAudioPlayerDelegateProtocol
//or
player.delegate = MyOwnAudioPlayerDelegateImpl()
The problem is that the delegate is nullified at random times. It works if I make the class containing the player extend
AvAudioPlayerDelegateProtocol
and then do
player.delegate = this
. But it's a bit ugly and creates different issues. Does anyone know how I can manage memory reliably in the first scenario?
✔️ 1
Okay, got it. Some ios classes keep their delegates as weak references. This applies to AvPlayer. The trick is to keep a strong reference to it somewhere else. Simply making it a member in the enclosing class makes it work fine.