Skip to content

Commit 01d2467

Browse files
committed
implement native finalizers and use them to auto-close query and property-query
1 parent d39bd14 commit 01d2467

File tree

10 files changed

+4128
-21
lines changed

10 files changed

+4128
-21
lines changed

objectbox/lib/src/native/bindings/bindings.dart

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'objectbox-c.dart';
88
export 'objectbox-c.dart';
99

1010
// ignore_for_file: public_member_api_docs
11+
// ignore_for_file: non_constant_identifier_names
1112

1213
/// Tries to use an already loaded objectbox dynamic library. This is the only
1314
/// option for macOS and iOS and is ~5 times faster than loading from file so
@@ -18,7 +19,8 @@ ObjectBoxC? _tryObjectBoxLibProcess() {
1819

1920
ObjectBoxC? obxc;
2021
try {
21-
obxc = ObjectBoxC(DynamicLibrary.process());
22+
_lib = DynamicLibrary.process();
23+
obxc = ObjectBoxC(_lib!);
2224
_isSupportedVersion(obxc); // may throw in case symbols are not found
2325
} catch (_) {
2426
// ignore errors (i.e. symbol not found)
@@ -27,19 +29,19 @@ ObjectBoxC? _tryObjectBoxLibProcess() {
2729
}
2830

2931
ObjectBoxC? _tryObjectBoxLibFile() {
30-
DynamicLibrary? lib;
32+
_lib = null;
3133
var libName = 'objectbox';
3234
if (Platform.isWindows) {
3335
libName += '.dll';
3436
try {
35-
lib = DynamicLibrary.open(libName);
37+
_lib = DynamicLibrary.open(libName);
3638
} on ArgumentError {
3739
libName = 'lib/' + libName;
3840
}
3941
} else if (Platform.isMacOS) {
4042
libName = 'lib' + libName + '.dylib';
4143
try {
42-
lib = DynamicLibrary.open(libName);
44+
_lib = DynamicLibrary.open(libName);
4345
} on ArgumentError {
4446
libName = '/usr/local/lib/' + libName;
4547
}
@@ -50,8 +52,8 @@ ObjectBoxC? _tryObjectBoxLibFile() {
5052
} else {
5153
return null;
5254
}
53-
lib ??= DynamicLibrary.open(libName);
54-
return ObjectBoxC(lib);
55+
_lib ??= DynamicLibrary.open(libName);
56+
return ObjectBoxC(_lib!);
5557
}
5658

5759
bool _isSupportedVersion(ObjectBoxC obxc) => obxc.version_is_at_least(
@@ -77,9 +79,8 @@ ObjectBoxC loadObjectBoxLib() {
7779
return obxc;
7880
}
7981

80-
ObjectBoxC? _cachedBindings;
81-
82-
ObjectBoxC get C => _cachedBindings ??= loadObjectBoxLib();
82+
DynamicLibrary? _lib;
83+
late final ObjectBoxC C = loadObjectBoxLib();
8384

8485
/// Init DartAPI in C for async callbacks.
8586
///
@@ -111,3 +112,12 @@ void initializeDartAPI() {
111112
// -1 => failed to initialize - incompatible Dart version
112113
int _dartAPIInitialized = 0;
113114
Object? _dartAPIInitException;
115+
116+
/// A couple of native functions we need as callbacks to pass back to native.
117+
/// Unfortunately, ffigen keeps those private.
118+
typedef _native_close = Int32 Function(Pointer<Void> ptr);
119+
120+
late final native_query_close =
121+
_lib!.lookup<NativeFunction<_native_close>>('obx_query_close');
122+
late final native_query_prop_close =
123+
_lib!.lookup<NativeFunction<_native_close>>('obx_query_prop_close');

0 commit comments

Comments
 (0)