-
Notifications
You must be signed in to change notification settings - Fork 56
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
App always crashes on hot restart #970
Comments
Later, I discovered that JNI crashes hard whenever closing the flutter engine (??) I discovered this because my app has a background If it fires at the same time I use the app, it launches it's own flutterEngine, does some stuff, and then closes it... which crashes the whole app:
I see the same crash when i swipe away my app (well, then it's not the problem obviously 😂) Any useful details: I am using And I, of course, don't |
About your original issue: You're doing _manager = android.BluetoothManager.fromRef(
ctx
.getSystemService(android.Context.BLUETOOTH_SERVICE.toJString())
.reference,
); to simplify, let's write it like: bar = Bar.fromRef(foo.reference); Here, both bar = foo.castTo(Bar.type); This will create a new reference for bar = foo.castTo(Bar.type, releaseOriginal: true); This way, the same underlying reference is used for So, in your case, do: _manager = ctx.getSystemService(android.Context.BLUETOOTH_SERVICE.toJString())
.castTo(android.BluetoothManager.type, releaseOriginal: true); |
thank you!!
Oh it works now!!! Both of my issues are gone ✌️ Big thanks again! |
@HosseinYousefi Does this issue arise due to us using inheritance over composition? More specifically I see that we have class JReference implements Finalizable {}
class JObject extends JReference {}
class JArray extends JObject {}
class X extends JObject {} It would seem more natural to model this as composition and attach finalizers to class JReference implements Finalizable {}
class JObject {
final JReference _reference;
}
class JArray extends JObject {}
class X extends JObject {} Though since we do allow eager releasing, any re-use of the reference must be made explicit - so developers have to decide (and know) whether reference is shared or not, and thereby be cautions about when to release. |
Yes, this exact problem will be solved, and I will change this in the next version (also to allow sharing of |
Looking forward to sending jobjects between Isolates because it will allow me for async read/writes to bluetooth - right now I'm literally doing while(true) {
if(available > 0) {
...
}
await Future.delayed(10ms); // free the main Isolate
} |
You can send them between isolates. It's just a bit manual right now: For example, using this helper method: Future<R> runOnIsolate<T extends JObject, R>(
T object, R Function(T object) callback) async {
final type = object.$type;
final address = object.reference.address;
return Isolate.run(() {
final ref = Jni.env.NewGlobalRef(Pointer.fromAddress(address));
final sentObject = type.fromRef(ref) as T;
return callback(sentObject);
});
} And then you can run something on a different isolate like: final str = 'hello'.toJString();
await runOnIsolate(str, (object) {
print(object);
}); // prints hello |
While hot-reload works fine, hot-restarting crashes my app every time with
JNI DETECTED ERROR IN APPLICATION: JNI ERROR (app bug): jobject is an invalid global reference: 0x30c6 (deleted reference at index 390) in call to DeleteGlobalRef
I will obviously show my code, but I think it's details are not useful because I suspect I must be "not properly freeing Java references" in
dispose()
es or something like that - so, I'm opening this issue as a request to add some universal notes about when and why to call.release()
🙏My main "plugin" file: https://github.com/TheLastGimbus/the_last_bluetooth/blob/60013e4959cb1f0ecd8c2d9f2e1daea81f4a60bd/lib/the_last_bluetooth.dart
My example's main.dart that uses it: https://github.com/TheLastGimbus/the_last_bluetooth/blob/60013e4959cb1f0ecd8c2d9f2e1daea81f4a60bd/example/lib/main.dart
The gigantic stack trace: https://pastebin.com/TRa3kyVu
The text was updated successfully, but these errors were encountered: