objectbox-dart
                                
                                 objectbox-dart copied to clipboard
                                
                                    objectbox-dart copied to clipboard
                            
                            
                            
                        Internal ObjectBox exceptions are not reported correctly
Operating system: Ubuntu 16.04 [to be confirmed for other OSs] Dart version: 2.4.1
When trying to call ObjectBox functions with e.g. invalid parameters, normally the error should be reported on the command line as well as being reflected as an error code retrieved from obx_last_error_code. As an example, try creating an entity with ID -1 (@Entity(id: -1, uid: 1)), which is illegal in ObjectBox. Instead of seeing a meaningful message, the following is output, in other cases with the OBX_ERROR_NO_ERROR_INFO error code:
001-10:36:16.1248 [ERROR] Cannot handle thrown exception, no ptr
After some investigation, it became obvious that this issue is due to this problem in the Dart FFI, i.e. something rather fundamental in C++.
From https://github.com/dart-lang/sdk/issues/38141#issuecomment-527622321:
Flutter may not have this issue, but if they do, they (and other embedders) need to address it separately.
Once #9 is done, we should test this on Flutter and report to the Flutter project if required.
works with dart 2.6(.1)
added a test case in 04e1ae0de2b9c834e851ca974629dac402d6cc3b
Turns out this doesn't work on Flutter for Linux (desktop), I've filed an issue: https://github.com/flutter/flutter/issues/74599
Since our original issue was closed ("fixed and then reverted"), the current one to watch is https://github.com/flutter/flutter/issues/66575. Is there any info we could provide to speed this up on the Flutter side? E.g. a generalized test case?
They specifically mention Linux as the broken platform. Do Windows and macOS do not expose the behavior?
Could we get around the problem by statically linked libs, e.g. for Linux?
The issue is specific to the Flutter linux build.
A statically linked libstdc++ doesn't help, still crashes during exception throw - we have a test case exactly for that in basics_test.dart, can be run as ~/lib/flutter/bin/dart run ./test/basics_test.dart.
For completeness, the usual libobjectbox.so dependencies and the ones with static libstdc++ linking:
$ ldd /usr/lib/libobjectbox.so 
        linux-vdso.so.1 (0x00007ffd00739000)
        libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x00007f34c2459000)
        libm.so.6 => /usr/lib/libm.so.6 (0x00007f34c2314000)
        libgcc_s.so.1 => /usr/lib/libgcc_s.so.1 (0x00007f34c22fa000)
        libpthread.so.0 => /usr/lib/libpthread.so.0 (0x00007f34c22d9000)
        libc.so.6 => /usr/lib/libc.so.6 (0x00007f34c210c000)
        /usr/lib64/ld-linux-x86-64.so.2 (0x00007f34c2997000)
$ ldd /usr/lib/libobjectbox.so 
        linux-vdso.so.1 (0x00007ffdb2995000)
        libm.so.6 => /usr/lib/libm.so.6 (0x00007ffa6c6de000)
        libpthread.so.0 => /usr/lib/libpthread.so.0 (0x00007ffa6c6bd000)
        libc.so.6 => /usr/lib/libc.so.6 (0x00007ffa6c4f0000)
        /usr/lib64/ld-linux-x86-64.so.2 (0x00007ffa6cff0000)
Not for now; has time for later: can we have fully static lib, not just libstdc++?
can we have fully static lib, not just libstdc++?
not a shared library AFAICT.
Check if this is still an issue (e.g. the linked Flutter issue is closed). Test our Flutter example app on Linux.
E.g. modify objectbox/example/flutter/objectbox_demo_relations/lib/objectbox.dart to put with negative ID:
task = Task(text, id: -100);
OK, finally confirmed this is fixed. Tested with Flutter 3.19, but this was probably fixed as early as August 2021.
Exception is thrown as expected:
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Invalid argument(s): object put failed: ID is higher or equal to internal ID sequence: 18446744073709551516 (vs. 4). Use ID 0 (zero) to insert new entities. (OBX_ERROR code 10002)
#0      ObjectBoxNativeError.throwMapped (package:objectbox/src/native/bindings/helpers.dart:76:9)
#1      throwLatestNativeError (package:objectbox/src/native/bindings/helpers.dart:54:48)
#2      Box._handlePutObjectResult (package:objectbox/src/native/box.dart:342:22)
#3      Box._put (package:objectbox/src/native/box.dart:252:10)
#4      Box.put.<anonymous closure> (package:objectbox/src/native/box.dart:95:53)
#5      Store._runInTransaction (package:objectbox/src/native/store.dart:776:24)
#6      InternalStoreAccess.runInTransaction (package:objectbox/src/native/store.dart:894:13)
#7      Box.put (package:objectbox/src/native/box.dart:94:34)
#8      Box._putAsyncCallback (package:objectbox/src/native/box.dart:103:22)
#9      _RunAsyncIsolateConfig.runCallback (package:objectbox/src/native/store.dart:959:51)
#10     Store._callFunctionWithStoreInIsolate (package:objectbox/src/native/store.dart:663:44)
#11     _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:300:17)
#12     _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)