android-ndk-gif icon indicating copy to clipboard operation
android-ndk-gif copied to clipboard

[NativeCrash] Crash after encoding ends

Open gmikhail opened this issue 2 years ago • 1 comments

Sometimes after encoding my app crashes at the end (when encoder.close() is called):

2023-09-16 15:27:48.589 13770-13965 libc                    my.app.id              A  FORTIFY: fwrite: null FILE*
2023-09-16 15:27:48.736 13770-13817 RenderInspector         my.app.id              W  DequeueBuffer time out on my.app.id/my.app.id.activities.CameraActivity, count=3, avg=21 ms, max=44 ms.
2023-09-16 15:27:48.945 13770-13965 libc                    my.app.id              A  Fatal signal 6 (SIGABRT), code -1 (SI_QUEUE) in tid 13965 (pool-16-thread-), pid 13770 (.app.id)
2023-09-16 15:27:49.523 14023-14023 DEBUG                   pid-14023                            A  Cmdline: my.app.id
2023-09-16 15:27:49.523 14023-14023 DEBUG                   pid-14023                            A  pid: 13770, tid: 13965, name: pool-16-thread-  >>> my.app.id <<<
2023-09-16 15:27:49.524 14023-14023 DEBUG                   pid-14023                            A        #03 pc 0000000000019eb4  /data/app/~~J1EVXNeWwwbMII5rmne23w==/my.app.id-egVwL-amUM-5O0nTU8DDmw==/base.apk!libandroidndkgif.so (SimpleGCTGifEncoder::encodeFrame(unsigned int*, int)+152) (BuildId: 1623855e354ccf7631cc06f66e3ffecb55974bff)
2023-09-16 15:27:49.524 14023-14023 DEBUG                   pid-14023                            A        #04 pc 000000000001584c  /data/app/~~J1EVXNeWwwbMII5rmne23w==/my.app.id-egVwL-amUM-5O0nTU8DDmw==/base.apk!libandroidndkgif.so (Java_com_waynejo_androidndkgif_GifEncoder_nativeEncodeFrame+152) (BuildId: 1623855e354ccf7631cc06f66e3ffecb55974bff)
2023-09-16 15:27:49.524 14023-14023 DEBUG                   pid-14023                            A        #07 pc 00000000004af080  /data/app/~~J1EVXNeWwwbMII5rmne23w==/my.app.id-egVwL-amUM-5O0nTU8DDmw==/base.apk
2023-09-16 15:27:49.524 14023-14023 DEBUG                   pid-14023                            A        #09 pc 0000000000002272  /data/data/my.app.id/code_cache/.overlay/base.apk/classes4.dex

Despite this, in most cases the gif file is working, but sometimes it is static, as in this issue https://github.com/waynejo/android-ndk-gif/issues/31

What could be the cause of the problem?


implementation 'io.github.waynejo:androidndkgif:1.0.1'

// NOTE: The code has been simplified for ease of reading

class GifEncoder {

    private lateinit var encoder: GifEncoder
	
    fun create(){
        encoder = GifEncoder()
        val storageDir = File(
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
                .toString() + "/APP_NAME")
        if (!storageDir.exists()) {
            storageDir.mkdirs()
        }
        val imageFileName = "APP_NAME_" + System.currentTimeMillis() + ".gif"
        val imageFile = File(storageDir, imageFileName)
        val savedImagePath = imageFile.absolutePath
        encoder.init(480, 640, savedImagePath, GifEncoder.EncodingType.ENCODING_TYPE_SIMPLE_FAST)
    }

    fun addFrame(bitmap: Bitmap, delay: Int){
        encoder.encodeFrame(bitmap, delay)
    }

    fun stop(){
        encoder.close()
    }
}

Memory consumption on average remains at 200 MB, and the CPU load does not exceed 30% on POCO X3 NFC.

gmikhail avatar Sep 16 '23 12:09 gmikhail

It looks like I found a possible cause of the crash.

If after encoder.encodeFrame you immediately call encoder.close() - it's going to crash. It looks like the library does not have enough time to process the last frame, and encoder.close() does not check the processing queue, but immediately releases resources which makes further processing of unprocessed frames impossible and leads to crash.

Could you please check my guess?

Right now I just set the delay after last encoder.encodeFrame call and before encoder.close(), but I would like a more elegant solution.

gmikhail avatar Sep 19 '23 16:09 gmikhail