bytedeco / javacv

Java interface to OpenCV, FFmpeg, and more
Other
7.6k stars 1.59k forks source link

Fatal error when calling MatOfByte.toArray() #2254

Closed ShioriSaito-asken closed 4 months ago

ShioriSaito-asken commented 4 months ago

Describe the issue

We encounter a SIGSEGV (0xb) error when calling MatOfByte.toArray(). This error occurs occasionally under high request loads (800 requests per minute for about 10 minutes). If you have any idea how to fix this, I would really appreciate it.

code

build.gradle.kts

    // https://mvnrepository.com/artifact/org.bytedeco/javacv
    implementation("org.bytedeco:javacv:1.5.10")
    // https://mvnrepository.com/artifact/org.bytedeco/javacv-platform
    implementation("org.bytedeco:javacv-platform:1.5.10")

We are resizing a JPG image and converting it to a ByteArray using Kotlin. Error log shows that the method below cause the error.

private fun minimize(mat: Mat, size: ImageSizePair): ByteArray {
        // The reduction ratio (base/original) adopts the smaller percentage in width-height to maintain the aspect ratio.
        return mat.let {
            val dist = resize(mat, size)
            MatOfByte().run {
                Imgcodecs.imencode(".jpg", dist, this)
                val array = this.toArray()
                array
            }
        } ?: throw IllegalArgumentException("")
    }

We have also added a PointerScope to the caller of this method, but the issue persists.

 fun resize(image: ByteArray, size: ImageSize): ByteArray {
        return PointerScope().use {
            val mat = Imgcodecs.imdecode(MatOfByte(*image), Imgcodecs.IMREAD_ANYCOLOR)
            when (size) {
                // ...
                ANALYZE -> minimize(mat, ANALYZE_IMAGE_SIZE)
            }
        }
    }

error log

I will attach the contents of the “hs_err_pid” file. hs_err_pid31.log

saudet commented 4 months ago

I would recommend using the C++ API, it's probably more stable

ShioriSaito-asken commented 4 months ago

Thank you for your response. We will consider it.