Fix potential memory leak in video player sample.

This commit is contained in:
Nikolay Igotti
2019-10-26 14:21:27 +03:00
parent 912eb4f1fa
commit fe5ef5bde9
2 changed files with 3 additions and 8 deletions
@@ -316,7 +316,7 @@ private class Decoder(
fun audioVideoSynced() = (audio?.isSynced() ?: true) || done() fun audioVideoSynced() = (audio?.isSynced() ?: true) || done()
} }
class DecoderWorker(val worker: Worker) : Disposable { inline class DecoderWorker(val worker: Worker) : Disposable {
// This class must have no other state, but this worker object. // This class must have no other state, but this worker object.
// All the real state must be stored on the worker's side. // All the real state must be stored on the worker's side.
constructor() : this(Worker.start()) constructor() : this(Worker.start())
@@ -65,15 +65,10 @@ class SDLAudio(val player: VideoPlayer) : DisposableContainer() {
} }
} }
// Only set in the audio thread
@ThreadLocal
private var decoder: DecoderWorker? = null
private fun audioCallback(userdata: COpaquePointer?, buffer: CPointer<Uint8Var>?, length: Int) { private fun audioCallback(userdata: COpaquePointer?, buffer: CPointer<Uint8Var>?, length: Int) {
// This handler will be invoked in the audio thread, so reinit runtime. // This handler will be invoked in the audio thread, so reinit runtime.
kotlin.native.initRuntimeIfNeeded() kotlin.native.initRuntimeIfNeeded()
val decoder = decoder ?: val decoder = DecoderWorker(Worker.fromCPointer(userdata))
DecoderWorker(Worker.fromCPointer(userdata)).also { decoder = it }
var outPosition = 0 var outPosition = 0
while (outPosition < length) { while (outPosition < length) {
val frame = decoder.nextAudioFrame(length - outPosition) val frame = decoder.nextAudioFrame(length - outPosition)