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?