objectbox / objectbox-dart

Flutter database for super-fast Dart object persistence
https://docs.objectbox.io/getting-started
Apache License 2.0
927 stars 115 forks source link

Dart 3.0: Code crashes when query is inside condition with "code removed by Dart AOT compiler" #555

Closed bananowysong closed 5 months ago

bananowysong commented 8 months ago

Description: Code crashes when query is inside condition with "code removed by Dart AOT compiler"

Basic info:

[✓] Android toolchain - develop for Android devices (Android SDK version 32.0.0) • Android SDK at • Platform android-33, build-tools 32.0.0 • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189) • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.2) • Xcode at /Applications/Xcode.app/Contents/Developer • Build 14C18 • CocoaPods version 1.12.1

[✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2020.3) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)

[✓] VS Code (version 1.81.1) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.70.0

[✓] Connected device (5 available)

[✓] Network resources • All expected network resources are available.

Steps to reproduce

TODO

  1. Put

    
    final Query<Whatever> query;
    if (condition) {
      query = dataBox
          .query(Model_.value.equals(id))
          .build();
    } else {
      query = dataBox.query(Model_.otherValue.equals(id)).build();
    }
    
    final data = query.find();
    query.close();
    return data;
    }
  2. In debug it will work, but in release version this code will crash, with: "Attempt to execute code removed by Dart AOT compiler". The function does not run at all.

Expected behavior

It would not crash and return data.

Additional context

Started after upgrading flutter to 3.13.2 from 3.1.

greenrobot-team commented 8 months ago

Thanks for reporting!

Do you have a stack trace of the crash? Does this also occur with the latest version of ObjectBox?

Maybe we can make use of some pragma annotations (though I don't see how that one would help here). Or this is a Dart bug which we should then report.

greenrobot-team commented 8 months ago

Confirmed this also occurs with the latest version. Could reproduce with our Flutter example.

Full output (will try to localize as line number is not shown):

E/flutter ( 4208): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Attempt to execute code removed by Dart AOT compiler (TFA)
E/flutter ( 4208): #0      new ObjectBox._create (package:objectbox_demo_relations/objectbox.dart)
E/flutter ( 4208): #1      ObjectBox.create (package:objectbox_demo_relations/objectbox.dart:67)
E/flutter ( 4208): <asynchronous suspension>
E/flutter ( 4208): #2      main (package:objectbox_demo_relations/main.dart:19)
E/flutter ( 4208): <asynchronous suspension>
E/flutter ( 4208): 
greenrobot-team commented 8 months ago

This is a known Dart issue which should be fixed with a future Dart release (likely 3.2.0). See also a related issue for workarounds.

@bananowysong An alternative to your workaround is to make query non-final and nullable, e.g.

Query<Whatever>? query;
if (condition) {
  query = dataBox
      .query(Model_.value.equals(id))
      .build();
} else {
  query = dataBox.query(Model_.otherValue.equals(id)).build();
}
final data = query!.find();
query!.close();

For other users, we do not recommend to update to Dart 3.0.0 (included as of Flutter 3.10.0) or later for the time being.

greenrobot-team commented 5 months ago

This issue is fixed since Dart 3.2.0 which shipped with Flutter 3.16.0. If you are affected, update to this or a newer version of Flutter.