RedApparat / Fotoapparat

Making Camera for Android more friendly. 📸
Apache License 2.0
3.81k stars 405 forks source link

Repeatedly calling fotoapparat.takePicture() causes app to crash #418

Open LaVlad opened 2 years ago

LaVlad commented 2 years ago

I'm making a simple Android app that needs to repeatedly make photos every second while it's operational. I delete the photos that are more than a minute old from the file system. This I achieve through roughly the following code:

timer = timerTask {
                val curFile = File(photoFolder, System.currentTimeMillis().toString() + ".png")
                fotoapparat?.takePicture()?.saveToFile(curFile)
                photoFiles.add(curFile)
                if (photoFiles.size > 60) {
                    photoFiles[0].delete()
                    photoFiles.removeFirst()
                }
            }
            Timer("SettingUp", false).scheduleAtFixedRate(timer, 2, 1000)

Everything works fine for a while, however the app consistently ceases to function after roughly 14 minutes of taking pictures. IDE doesn't throw any error upon termination, it just stops. Moreover, after that if the app is launched again it will crash shortly after the launch. This is not fixed by deinstalling and reinstalling the app or restarting the emulator. The only fix I was able to discover is wiping user data from the emulator.

After some testing and experimentation I was able to narrow the issue down to the line where pictures are taken (if this line is commented the app works indefinitely just fine):

fotoapparat?.takePicture()?.saveToFile(curFile)

Moreover, upon more experimentation I discovered, that even after disabling saving the photos (the saveToFile() part), calling fotoapparat.takePicture() alone still leads to the same effect within the same timespan.

I've monitored the app through Android Studio memory profiler and discovered the steady increase in "Others" memory consumption throughout the process, seemingly displacing other memory categories, until the big spike leads to app's crash.

image

How did you initialize FA?

var fotoapparat: Fotoapparat? = null
var fotoapparatState : FotoapparatState? = null

public override fun onCreate(state: Bundle?) {
        super.onCreate(state)

        createFotoapparat()
        fotoapparatState = FotoapparatState.OFF
}

private fun createFotoapparat(){
    val cameraView = findViewById<CameraView>(R.id.camera_view)

    fotoapparat = Fotoapparat(
        context = this,
        view = cameraView,
        scaleType = ScaleType.CenterCrop,
        lensPosition = back(),
        logger = loggers(
            logcat()
        ),
        cameraErrorCallback = { error ->
            println("Recorder errors: $error")
        }
    )
}

override fun onStart() {
    super.onStart()
    fotoapparat?.start()
    fotoapparatState = FotoapparatState.ON
}

What was the result you received?

The app crashes without errors after approximately 14 minutes of consecutively taking pictures.

What did you expect?

App proceeds to operate indefinitely.

Context: