Skip to content

Either hide or heavily discourage the use of reference and fromRef #979

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
HosseinYousefi opened this issue Mar 5, 2024 · 12 comments
Closed

Comments

@HosseinYousefi
Copy link
Member

Although safe methods like castTo are provided, users might still use fromRef and .reference to manually cast objects (see #970), keep references without properly managing their lifecycle, etc. Therefore, JniGen generated code should heavily discourage the use of these inner workings and eventually hide them. Right now, we need references to send these finalizable JObjects between multiple isolates, but dart-lang/sdk#55062 will make these objects sendable.

cc/ @dcharkes

@yanshouwang
Copy link

yanshouwang commented Mar 12, 2024

Hello, @HosseinYousefi
I found here you mentioned that we can send a JObject between isolates, but when I send the reference to another isolate and call formRef constructor, the first time it's ok to do this, but when I send this reference again, the fromRef method will throw error like this:

signal 6 (SIGABRT), code -1 (SI_QUEUE), fault addr --------
Abort message: 'JNI DETECTED ERROR IN APPLICATION: JNI ERROR (app bug): jobject is an invalid global reference: 0x357a (deleted reference at index 427)

Is it possible for us to send a JObject to another isolate?

@HosseinYousefi
Copy link
Member Author

I have explained one way to send them here: #970 (comment)

@yanshouwang
Copy link

@HosseinYousefi
Thank you for your answer, that's works like a charm~

@HosseinYousefi
Copy link
Member Author

@HosseinYousefi Thank you for your answer, that's works like a charm~

With the newly released 0.8.0, you can skip the manual way (in Dart's main branch – not yet in stable) and just pass JObjects around as you would with any other object.

@yanshouwang
Copy link

yanshouwang commented Apr 12, 2024

With the newly released 0.8.0, you can skip the manual way (in Dart's main branch – not yet in stable) and just pass JObjects around as you would with any other object.

@HosseinYousefi , I have updated to 0.8.0, when I pass the JObject to an isolate, the error throws, I use the flutter stable channel now.

E/flutter (13009): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Invalid argument(s): Illegal argument in isolate message: (object is a Pointer)
E/flutter (13009):  <- Instance of 'JGlobalReference' (from package:jni/src/jreference.dart)
E/flutter (13009):  <- $type in Instance of 'BarcodeScanner' (from package:hybrid_vision_android/src/jni.g.dart)
E/flutter (13009):  <- scanner in Instance of '_DetectCommand' (from package:hybrid_vision_android/src/barcode_detection.dart)
E/flutter (13009): 
E/flutter (13009): #0      _SendPort._sendInternal (dart:isolate-patch/isolate_patch.dart:250:43)
E/flutter (13009): #1      _SendPort.send (dart:isolate-patch/isolate_patch.dart:231:5)
E/flutter (13009): #2      AndroidBarcodeDetector.detect (package:hybrid_vision_android/src/barcode_detection.dart:51:14)
E/flutter (13009): <asynchronous suspension>
E/flutter (13009): #3      _BarcodeDetectionViewState.buildDetectView.<anonymous closure>.<anonymous closure> (package:hybrid_vision_android_example/views/barcode_detection_view.dart:173:38)
E/flutter (13009): <asynchronous suspension>
E/flutter (13009): 

@HosseinYousefi
Copy link
Member Author

With the newly released 0.8.0, you can skip the manual way (in Dart's main branch – not yet in stable) and just pass JObjects around as you would with any other object.

@HosseinYousefi , I have updated to 0.8.0, when I pass the JObject to an isolate, the error throws, I use the flutter stable channel now.

E/flutter (13009): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Invalid argument(s): Illegal argument in isolate message: (object is a Pointer)
E/flutter (13009):  <- Instance of 'JGlobalReference' (from package:jni/src/jreference.dart)
E/flutter (13009):  <- $type in Instance of 'BarcodeScanner' (from package:hybrid_vision_android/src/jni.g.dart)
E/flutter (13009):  <- scanner in Instance of '_DetectCommand' (from package:hybrid_vision_android/src/barcode_detection.dart)
E/flutter (13009): 
E/flutter (13009): #0      _SendPort._sendInternal (dart:isolate-patch/isolate_patch.dart:250:43)
E/flutter (13009): #1      _SendPort.send (dart:isolate-patch/isolate_patch.dart:231:5)
E/flutter (13009): #2      AndroidBarcodeDetector.detect (package:hybrid_vision_android/src/barcode_detection.dart:51:14)
E/flutter (13009): <asynchronous suspension>
E/flutter (13009): #3      _BarcodeDetectionViewState.buildDetectView.<anonymous closure>.<anonymous closure> (package:hybrid_vision_android_example/views/barcode_detection_view.dart:173:38)
E/flutter (13009): <asynchronous suspension>
E/flutter (13009): 

As I said, it's not available on stable channel yet. Use the main channel.

@yanshouwang
Copy link

As I said, it's not available on stable channel yet. Use the main channel.

Is there a exact Dart version that I can do this on stable channel?

@HosseinYousefi
Copy link
Member Author

Is there a exact Dart version that I can do this on stable channel?

@dcharkes In which version will @pragma('vm:deeply-immutable') land?

@dcharkes
Copy link
Collaborator

Support landed in Dart in:

@yanshouwang
Copy link

Thanks, I'll wait for this feature, Can I use the JGlobalReference to do the manual steps like before?

@HosseinYousefi
Copy link
Member Author

Thanks, I'll wait for this feature, Can I use the JGlobalReference to do the manual steps like before?

Yes, you can.

@HosseinYousefi HosseinYousefi moved this from Todo to In Progress in JNIgen tracker Apr 23, 2024
@HosseinYousefi
Copy link
Member Author

Not currently planned as we still need reference for certain Jni.env functionalities that don't exist in a nicer way.

@HosseinYousefi HosseinYousefi closed this as not planned Won't fix, can't repro, duplicate, stale Apr 29, 2024
@github-project-automation github-project-automation bot moved this from In Progress to Done in JNIgen tracker Apr 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

No branches or pull requests

3 participants