dart-archive / ffi

Utilities for working with Foreign Function Interface (FFI) code
https://pub.dev/packages/ffi
BSD 3-Clause "New" or "Revised" License
155 stars 32 forks source link

Unable to use callback in c code #55

Closed Dolaned closed 3 years ago

Dolaned commented 3 years ago

Hi i am trying to call a callback from C but it throws a sigarbt, i currently have another callback that works. I am running: Dart SDK version: 2.10.2 (stable) (Tue Oct 13 15:50:27 2020 +0200) on "macos_x64"

sigabrt

Ill attach the typedef and the function too i am using dart/ffigen to create the library headers

saveblocks typedef saveblocks

i have a function that only returns a void and that works fine.

thanks again

Dolaned commented 3 years ago

@dcharkes this is what i was referring to in the other thread

dcharkes commented 3 years ago

What thread are you trying to do the callback on? (Thread 27 does not look like a Dart execution thread, with no DartVM things on the stack and a pthread_start.)

You can only do a callback into an isolate with the main Dart execution thread from that isolate. Doing a callback from any other thread results in an abort. This abort is intentional, it is not supported to run multiple threads in one Dart isolate.

If you want to use a thread that you spawn in C, you have to use the message passing system (native ports) to communicate back to Dart:

  Dart_Port send_port;

  Dart_CObject dart_object;
  dart_object.type = Dart_CObject_kInt64;
  dart_object.value.as_int64 = work_addr;

  const bool result = Dart_PostCObject_DL(send_port, &dart_object);
Dolaned commented 3 years ago

ok im not running a seperate isolate from the main one but this still doesnt matter right?

dcharkes commented 3 years ago

No, you're starting a thread in C (_pthread_start) and doing an FFI callback back into Dart, that is not supported.

Greg-Griffith commented 3 years ago

Thank you @dcharkes. Your explanation makes sense. the saveBlocks function pictured above is called by a function that was called from dart, which internally spawned a new C thread which then responds to that callback.

Dolaned commented 3 years ago

so to clarify we would actually put them callbacks in C and then call native ports from them call backs?

Dolaned commented 3 years ago

hi mate is there some documentation on the c side of this? or is it just in the code examples? Thanks.

Edit: i have found this https://www.dynamsoft.com/codepool/dart-native-extension-barcode-sdk.html what would you recommend i do for a callback that is fired from C's side rather than dart making the call to C. Thanks.

Dolaned commented 3 years ago

Hi @dcharkes I think I'm beginning to understand it, one final question though

Is the result from the callback function supposed to come from the callback and the dart port is used to notify dart there is a result or is it all supposed to come from the dart port

Also, how do I compile this for Xcode it tells me it can't find the symbols for arm64 when including the header files

Thanks

Dolaned commented 3 years ago

@dcharkes is the dart sdk stripped back for flutter and that's why it can't find the symbols ?

dcharkes commented 3 years ago

@Dolaned - that's why you use the _DL versions of the symbols. See https://github.com/dart-lang/sdk/commit/7eac9f355ed54cb0aab018814f776c08d20cef44.

dcharkes commented 3 years ago

I'm assuming this is resolved by using the native ports with the _DL symbols. Please reopen if not.

Dolaned commented 3 years ago

yes that did fix the issue, thanks.