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()
}
class DecoderWorker(val worker: Worker) : Disposable {
inline class DecoderWorker(val worker: Worker) : Disposable {
// This class must have no other state, but this worker object.
// All the real state must be stored on the worker's side.
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) {
// This handler will be invoked in the audio thread, so reinit runtime.
kotlin.native.initRuntimeIfNeeded()
val decoder = decoder ?:
DecoderWorker(Worker.fromCPointer(userdata)).also { decoder = it }
val decoder = DecoderWorker(Worker.fromCPointer(userdata))
var outPosition = 0
while (outPosition < length) {
val frame = decoder.nextAudioFrame(length - outPosition)
@@ -88,4 +83,4 @@ private fun audioCallback(userdata: COpaquePointer?, buffer: CPointer<Uint8Var>?
break
}
}
}
}