Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Early return in Jetpack Compose view will crash with `java.lang.IllegalStateException: Start/end imbalance`?

I have a simple Composable as below

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            Testing()
        }
    }
}

@Composable
fun Testing(modifier: Modifier = Modifier) {
    var height = 0f
    Column(modifier = modifier.onGloballyPositioned {
        height = it.size.height.toFloat()
    }) {
        if (height == 0f) return
        // Do something
    }
}

It will crash with

2021-04-06 20:09:14.132 8383-8383/com.example.jetpackcomposeanimationspec E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.jetpackcomposeanimationspec, PID: 8383
    java.lang.IllegalStateException: Start/end imbalance
        at androidx.compose.runtime.ComposerImpl.finalizeCompose(Composer.kt:2890)
        at androidx.compose.runtime.ComposerImpl.endRoot(Composer.kt:1149)
        at androidx.compose.runtime.ComposerImpl.composeContent$runtime_release(Composer.kt:2602)
        at androidx.compose.runtime.CompositionImpl.composeContent(Composition.kt:348)
        at androidx.compose.runtime.Recomposer.composeInitial$runtime_release(Recomposer.kt:693)
        at androidx.compose.runtime.CompositionImpl.setContent(Composition.kt:304)

But if I change Testing to

@Composable
fun Testing(modifier: Modifier = Modifier) {
    var height = 0f
    Column(modifier = modifier.onGloballyPositioned {
        height = it.size.height.toFloat()
    }) {
        if (height != 0f) { 
            // Do something
        }
    }
}

It no longer crash. Why?

like image 725
Elye Avatar asked Oct 20 '25 14:10

Elye


1 Answers

I have this issue on Compose version 1.1.1. But if I type for example return@Column instead of return there will not be a crash.

Column {
    return // will crash
}

Column {
    return@Column // works for me
}

UPD! This works stable only in debug mode. If I build release it may crash with ArrayIndexOutOfBoundsException in ComposerImpl:

java.lang.ArrayIndexOutOfBoundsException: length=163; index=-1
        at java.util.ArrayList.remove(ArrayList.java:506)
        at androidx.compose.runtime.Stack.pop(Stack.kt:26)
        at androidx.compose.runtime.ComposerImpl.exitGroup(Composer.kt:2131)
        at androidx.compose.runtime.ComposerImpl.end(Composer.kt:2293)
        at androidx.compose.runtime.ComposerImpl.endGroup(Composer.kt:1494)
        at androidx.compose.runtime.ComposerImpl.endRoot(Composer.kt:1387)
        at androidx.compose.runtime.ComposerImpl.doCompose(Composer.kt:3199)
        at androidx.compose.runtime.ComposerImpl.recompose$runtime_release(Composer.kt:3140)
        at androidx.compose.runtime.CompositionImpl.recompose(Composition.kt:722)
        at androidx.compose.runtime.Recomposer.performRecompose(Recomposer.kt:876)
        at androidx.compose.runtime.Recomposer.access$performRecompose(Recomposer.kt:107)
        at androidx.compose.runtime.Recomposer$runRecomposeAndApplyChanges$2$2.invoke(Recomposer.kt:485)
        at androidx.compose.runtime.Recomposer$runRecomposeAndApplyChanges$2$2.invoke(Recomposer.kt:454)
        at androidx.compose.ui.platform.AndroidUiFrameClock$withFrameNanos$2$callback$1.doFrame(AndroidUiFrameClock.android.kt:34)
        at androidx.compose.ui.platform.AndroidUiDispatcher.performFrameDispatch(AndroidUiDispatcher.android.kt:109)
        at androidx.compose.ui.platform.AndroidUiDispatcher.access$performFrameDispatch(AndroidUiDispatcher.android.kt:41)
        at androidx.compose.ui.platform.AndroidUiDispatcher$dispatchCallback$1.doFrame(AndroidUiDispatcher.android.kt:69)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1035)
        at android.view.Choreographer.doCallbacks(Choreographer.java:845)
        at android.view.Choreographer.doFrame(Choreographer.java:775)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1022)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.app.ActivityThread.main(ActivityThread.java:7839)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
        

In compose version '1.2.0-beta01' this issue still exists

like image 69
badadin Avatar answered Oct 23 '25 02:10

badadin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!