realm-dart icon indicating copy to clipboard operation
realm-dart copied to clipboard

uninitialized model throws: Attempt to execute code removed by Dart AOT compiler (TFA) when compiled to exe

Open KhaledBasiony opened this issue 2 years ago • 5 comments

What happened?

I need to have an uninitialized model (let's call it Product) like this:

final Product prod;

if (something) {
    prod = Product('prod 1');
} else {
    prod = Product('prod 2');
}

the problem appears when I compile the app using dart compile exe ... and run it, it throws that error: Attempt to execute code removed by Dart AOT compiler (TFA)

Notes:

  • It DOESN'T work whenever I specify the type Product
  • It DOES work if initialize it immediately (final prod = Product('prod');)
  • It DOES work if I don't use a type (final prod; for example)

I think I can work around it for now, I am just hoping this would get fixed :grinning:

if this not the appropriate place for this issue please guide me

Repro steps

  1. dart create dummy_project
  2. dart pub add realm_dart
  3. dart run realm_dart install
  4. create a file lib/model.dart with the following contents:
import 'package:realm_dart/realm.dart';

    part 'model.g.dart';
    
    @RealmModel()
    class _Product {}
    ```1. `dart run realm_dart generate`
1. put the following code in `bin/dummy_project.dart`:
```dart
import 'package:dummy_project/model.dart';

    void main(List<String> arguments) {
      final Product prod;
      prod = Product();
      print(prod);
    }
    ```1. `dart compile exe bin/dummy_project.dart -o bin/out`
1. `./bin/out`
1. panic :/
```sh
Unhandled exception:
Attempt to execute code removed by Dart AOT compiler (TFA)
#0      main (file:///home/khaled/Documents/dummy_project/bin/dummy_project.dart)
#1      _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:294)
#2      _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:189)

Version

Dart SDK version: 3.1.0 (stable) (Tue Aug 15 21:33:36 2023 +0000) on "linux_x64"

What Atlas Services are you using?

Local Database only

What type of application is this?

Dart standalone application

Client OS and version

OS: openSUSE Tumbleweed x86_64

Code snippets

final Product prod;

if (something) {
    prod = Product('prod 1');
} else {
    prod = Product('prod 2');
}

Stacktrace of the exception/crash you're getting

Unhandled exception:
Attempt to execute code removed by Dart AOT compiler (TFA)
#0      main (file:///home/khaled/Documents/dummy_project/bin/dummy_project.dart)
#1      _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:294)
#2      _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:189)

Relevant log output

No response

KhaledBasiony avatar Oct 17 '23 14:10 KhaledBasiony

Related to https://github.com/dart-lang/sdk/issues/52874

Dont make the product final. Make it nullable

final Product prod; to Product? prod;

milindgoel15 avatar Oct 21 '23 17:10 milindgoel15

Hello @milindgoel15, thanks for the comment.

I already worked around it by using "late"

But really, I don't want to make it nullable when it's not, and I don't want to use "late" and lose the static analysis/lints, and obviously I don't want it to be dynamic.

I just thought it would be nice for this particular case to be solved.

Thanks anyway.

KhaledBasiony avatar Oct 21 '23 18:10 KhaledBasiony

You could do ternary check instead of assigning them in the if else.

final Product product = something ? First : second

milindgoel15 avatar Oct 21 '23 18:10 milindgoel15

Huh, clever!

Unfortunately my actual code is a bit more complicated than a simple boolean check.

Will try to make it work tho, thanks!

KhaledBasiony avatar Oct 21 '23 18:10 KhaledBasiony

Huh, clever!

Unfortunately my actual code is a bit more complicated than a simple boolean check.

Will try to make it work tho, thanks!

If its a state, perhaps try https://riverpod.dev/, maybe a stream as then you can avoid nullable via an empty stream

dotjon0 avatar Oct 23 '23 10:10 dotjon0

There was a number of dart compiler bugs introduced with Dart 3 in relation to classes marked Finalizable (such as RealmObject subclasses). This is fixed by upgrading to 3.3 or later (sees https://github.com/realm/realm-dart/pull/1648).

nielsenko avatar May 06 '24 07:05 nielsenko