Skip to content

Commit 9801adf

Browse files
committed
ensure memory is always freed
1 parent 9ef35c1 commit 9801adf

File tree

6 files changed

+1541
-1346
lines changed

6 files changed

+1541
-1346
lines changed

lib/askar/askar_callbacks.dart

+9-17
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,15 @@ base class Callback<T extends Function> {
3232
final int id;
3333
final NativeCallable<T> nativeCallable;
3434
final Completer<AskarCallbackResult> completer;
35-
final void Function() cleanupPointers;
3635

37-
Callback(this.nativeCallable, this.completer, this.id, this.cleanupPointers);
36+
Callback(this.nativeCallable, this.completer, this.id);
3837

3938
Future<AskarCallbackResult> handleResult(int initialResult) {
4039
final initialErrorCode = ErrorCode.fromInt(initialResult);
4140

4241
if (initialErrorCode != ErrorCode.success) {
4342
completer.complete(AskarCallbackResult(initialErrorCode, false, null));
4443

45-
this.cleanupPointers();
4644
this.nativeCallable.close();
4745
}
4846

@@ -58,62 +56,58 @@ int nextCallbackId() {
5856

5957
typedef CbFuncWithHandle = Void Function(NativeCallbackId, Int32, NativeSessionHandle);
6058

61-
Callback<CbFuncWithHandle> newCallbackWithHandle(void Function() cleanup) {
59+
Callback<CbFuncWithHandle> newCallbackWithHandle() {
6260
final completer = Completer<AskarCallbackResult>();
6361

6462
late final NativeCallable<CbFuncWithHandle> nativeCallable;
6563

6664
void callback(int callbackId, int errorCode, int handle) {
6765
completer.complete(AskarCallbackResult(ErrorCode.fromInt(errorCode), true, handle));
68-
cleanup();
6966
nativeCallable.close();
7067
}
7168

7269
nativeCallable = NativeCallable<CbFuncWithHandle>.listener(callback);
7370

74-
return Callback<CbFuncWithHandle>(nativeCallable, completer, nextCallbackId(), cleanup);
71+
return Callback<CbFuncWithHandle>(nativeCallable, completer, nextCallbackId());
7572
}
7673

7774
typedef CbFuncWithInt64 = Void Function(NativeCallbackId, Int32, Int64);
7875

79-
Callback<CbFuncWithInt64> newCallbackWithInt64(void Function() cleanup) {
76+
Callback<CbFuncWithInt64> newCallbackWithInt64() {
8077
final completer = Completer<AskarCallbackResult>();
8178

8279
late final NativeCallable<CbFuncWithInt64> nativeCallable;
8380

8481
void callback(int callbackId, int errorCode, int handle) {
8582
completer.complete(AskarCallbackResult(ErrorCode.fromInt(errorCode), true, handle));
86-
cleanup();
8783
nativeCallable.close();
8884
}
8985

9086
nativeCallable = NativeCallable<CbFuncWithInt64>.listener(callback);
9187

92-
return Callback<CbFuncWithInt64>(nativeCallable, completer, nextCallbackId(), cleanup);
88+
return Callback<CbFuncWithInt64>(nativeCallable, completer, nextCallbackId());
9389
}
9490

9591
typedef CbFuncWithoutHandle = Void Function(NativeCallbackId, Int32);
9692

97-
Callback<CbFuncWithoutHandle> newCallbackWithoutHandle(void Function() cleanup) {
93+
Callback<CbFuncWithoutHandle> newCallbackWithoutHandle() {
9894
final completer = Completer<AskarCallbackResult>();
9995

10096
late final NativeCallable<CbFuncWithoutHandle> nativeCallable;
10197

10298
void callback(int callbackId, int errorCode) {
10399
completer.complete(AskarCallbackResult(ErrorCode.fromInt(errorCode), true, null));
104-
cleanup();
105100
nativeCallable.close();
106101
}
107102

108103
nativeCallable = NativeCallable<CbFuncWithoutHandle>.listener(callback);
109104

110-
return Callback<CbFuncWithoutHandle>(
111-
nativeCallable, completer, nextCallbackId(), cleanup);
105+
return Callback<CbFuncWithoutHandle>(nativeCallable, completer, nextCallbackId());
112106
}
113107

114108
typedef CbFuncWithPtrUft8 = Void Function(NativeCallbackId, Int32, Pointer<Utf8>);
115109

116-
Callback<CbFuncWithPtrUft8> newCallbackWithPtrUtf8(void Function() cleanup) {
110+
Callback<CbFuncWithPtrUft8> newCallbackWithPtrUtf8() {
117111
final completer = Completer<AskarCallbackResult>();
118112

119113
late final NativeCallable<CbFuncWithPtrUft8> nativeCallable;
@@ -122,12 +116,10 @@ Callback<CbFuncWithPtrUft8> newCallbackWithPtrUtf8(void Function() cleanup) {
122116
completer.complete(AskarCallbackResult(ErrorCode.fromInt(errorCode), true,
123117
utf8 == nullptr ? null : utf8.toDartString()));
124118
calloc.free(utf8);
125-
cleanup();
126119
nativeCallable.close();
127120
}
128121

129122
nativeCallable = NativeCallable<CbFuncWithPtrUft8>.listener(callback);
130123

131-
return Callback<CbFuncWithPtrUft8>(
132-
nativeCallable, completer, nextCallbackId(), cleanup);
124+
return Callback<CbFuncWithPtrUft8>(nativeCallable, completer, nextCallbackId());
133125
}

0 commit comments

Comments
 (0)