-
Notifications
You must be signed in to change notification settings - Fork 52
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
Callback with ObjCBlock.listener crashed with EXC_BAD_ACCESS
error
#1017
Comments
When you're interacting with native code, you need to think carefully about memory management. My guess is that your ObjCBlock is being garbage collected, because you're not holding a reference to it. Try sticking the block in a static variable. I know that's not the cleanest solution, but it's just to confirm that this is the bug. If that fixes it, you can come up with a cleaner way of holding on to the reference, but the details will depend on your use case (eg, can you share an single block instance for the whole app? Does the block need to fire only once?). |
I noticed here is an related issue about ObjCBlock.lisenter, Do we need some extra code to make it work when the callback has arguments? |
Maybe. #835 is about the callback arguments. If you've tried what I suggested before, and are holding a reference to the So the next thing to figure out is where exactly the crash is happening. If you aren't getting useful stack traces, you can try print debugging. For example, here's an ObjCBlock_ffiVoid_ObjCObject_bool.listener(
AVFAudio lib, void Function(NSObject, ffi.Pointer<ffi.Bool>) fn)
: this._(
lib._newBlock1(
(_dartFuncListenerTrampoline ??= ffi.NativeCallable<
ffi.Void Function(
ffi.Pointer<_ObjCBlock>,
ffi.Pointer<ObjCObject>,
ffi.Pointer<ffi.Bool>)>.listener(
_ObjCBlock_ffiVoid_ObjCObject_bool_closureTrampoline)
..keepIsolateAlive = false)
.nativeFunction
.cast(),
_ObjCBlock_ffiVoid_ObjCObject_bool_registerClosure(
(ffi.Pointer<ObjCObject> arg0, ffi.Pointer<ffi.Bool> arg1) =>
fn(NSObject._(arg0, lib, retain: true, release: true),
arg1))),
lib); The (ffi.Pointer<ObjCObject> arg0, ffi.Pointer<ffi.Bool> arg1) {
print("Creating wrapper object...");
final arg0Wrap = NSObject._(arg0, lib, retain: true, release: true);
print("Calling the function...");
return fn(arg0Wrap, arg1);
}, If this is where the crash is, then the fix is to retain a reference on the ObjC side, before the block is invoked. This is what #835 plans to do automatically, but you can do this manually if you don't want to wait. Just write an ObjC function that takes a block, and returns a block that retains a ref to the arg before calling the given block. Then you can pass your Dart block through that function to get a block that won't crash. One AI I already have from this issue is to make this more debuggable. It'd be nice if we got a clear error message if the arg has been GC'd, rather than just crashing. |
There is the logs, I think this crash is caused by the arguments
I tried the pedometer example, it works fine, but if I add
I add Is there anything I can do to make it work? |
The pedometer example is unrelated. To fix the argument ref counting issue, you'll need to retain a reference to the argument before sending it to Dart. To do this, you'll need to write a bit of ObjC code to wrap your callback in another callback that retains the arg then calls the original callback. |
I did write the objc code to wrap the callback like this
It throws when I use the wrapCallback method, I think this is caused by the I also write another implementation use the But I encountered another issue is that when I call an property with
The example with The example with |
Is this a bug or I missed something? The |
Oh sorry, I misspoke. It's not that 2 more ideas to help you debug this:
If 1 and 2 both work, then your |
Ah, you are right, according the Apple's docs, the |
App crashed when the callback method executed with
EXC_BAD_ACCESS
error.Related code:
The source repo is here: https://github.com/yanshouwang/hymir.dev/tree/hymir_mlkit/hymir_mlkit/hymir_mlkit_darwin
The text was updated successfully, but these errors were encountered: