How to pass array of structs from C++ to Dart

1,503 views
Skip to first unread message

rashedmyt

unread,
Oct 9, 2019, 7:11:44 AM10/9/19
to Dart FFI
I have been trying this for sometime but it always results in seg fault.

The C++ Code:

struct Test {
    int32_t data;
};

extern "C" __attribute__((visibility("default"))) __attribute__((used))
struct Test **getTestData() {
    struct Test **test = (struct Test **) malloc(4*sizeof(struct Test *));
    for (int i = 0; i < 4; i++) test[i] = (struct Test *) malloc(sizeof(struct Test));
    test[0]->data = 21;
    test[1]->data = 14;
    test[2]->data = 50;
    test[3]->data = 34;
    return test;
}

The Dart Code:

import 'dart:ffi';

class Test extends Struct<Test> {
  @Int32()
  int data;

  factory Test.allocate(int data) =>
      Pointer<Test>.allocate().load<Test>()..data = data;
}

final Pointer<Pointer<Test>> Function() nativeTest = cryptoLib
    .lookup<NativeFunction<Pointer<Pointer<Test>> Function()>>("getTestData")
    .asFunction();

String welcome() {
    final res = nativeTest();
    List<String> msgs = List<String>();
    var len = 0;

    while (true) {
        final temp = res.elementAt(len++).load<Pointer<Test>>();
        if (temp == nullptr) break; // I think the problem is here
        msgs.add(temp.load<Test>().data.toString());
    }

    return msgs.join(" ");
}

Whenever I call welcome function in my flutter app.. the app crashes with seg fault. Is there any other way of doing this correctly?

Daco Harkes

unread,
Oct 9, 2019, 7:17:17 AM10/9/19
to rashedmyt, Dart FFI
Hey Chandu,

Shouldn't you null-terminate the data structure on the C side?

  test[0]->data = 21;
    test[1]->data = 14;
    test[2]->data = 50;
    test[3]->data = 34;
    test[4] = nullptr;

(and malloc 5*size etc.)

Kind regards,

--
You received this message because you are subscribed to the Google Groups "Dart FFI" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dart-ffi+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dart-ffi/d210ae9c-22f4-4af7-835a-77ee4c3815ed%40googlegroups.com.


--
Daco Harkes

rashedmyt

unread,
Oct 9, 2019, 7:57:57 AM10/9/19
to Dart FFI
That worked.. Thanks. I was also wondering how to read a tuple in c++ on the dart side? I have a vector of tuple's which will be returned from the c++ to the dart. When I put extern "C", the compiler gave error's about conversion of vector to C-style arrays.. So I decided that i need to use pointer to send array's, but I couldn't understand how tuple is read on the dart side..
To unsubscribe from this group and stop receiving emails from it, send an email to dart...@googlegroups.com.


--
Daco Harkes

Daco Harkes

unread,
Oct 9, 2019, 8:14:46 AM10/9/19
to rashedmyt, Dart FFI
Hey Chandu,

std::tuple and std::vector are not part of the ABI (and can be laid out differently in memory by different compilers).
In dart:ffi, we only consider things specified by the ABI.

The work around is to write some C functions to interact with your data structures and pass a pointer to the data structure around.

Kind regards,


To unsubscribe from this group and stop receiving emails from it, send an email to dart-ffi+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dart-ffi/6a3eb6ef-43c3-43c8-b7e5-8ac328609505%40googlegroups.com.


--
Daco Harkes

Daco Harkes

unread,
Oct 9, 2019, 8:22:05 AM10/9/19
to rashedmyt, Dart FFI
FYI: I created https://github.com/dart-lang/sdk/issues/38788 to point people to. (This is not the first time this question has been asked.)
--
Daco Harkes

rashedmyt

unread,
Oct 9, 2019, 8:23:16 AM10/9/19
to Dart FFI
Oh.. ok. Thanks for pointing out the nullptr in C
Reply all
Reply to author
Forward
0 new messages