You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The using((arena) => ...) function provided by jni is great for ensuring that all references are disposed at the end of the code block.
There are many instances where the arena is not used to dispose the reference.
When using:
booleanValue(releaseOriginal: true)
intValue(releaseOriginal: true)
toDartString(releaseOriginal: true)
etc.
It is quite easy to mistakenly use a releaseBy(...) in conjunction with one of these functions and get yourself a DoubleReleaseError.
However using provides no helpful information about where this 2nd release is registered.
Possible Solutions
Don't throw on double release
My personal favorite
My Custom Function
import'dart:ffi';
import'package:ffi/ffi.dart';
/// Runs [computation] with a new [Arena], and releases all allocations at the/// end.////// If the return value of [computation] is a [Future], all allocations are/// released when the future completes.////// If the isolate is shut down, through `Isolate.kill()`, resources are _not_/// cleaned up./// /// Does not throw DoubleReleaseError.RmyUsing<R>(
RFunction(Arena) computation, [
Allocator wrappedAllocator = calloc,
]) {
final arena =Arena(wrappedAllocator);
var isAsync =false;
try {
final result =computation(arena);
if (result isFuture) {
isAsync =true;
return result.whenComplete(() {
try {
arena.releaseAll();
} onDoubleReleaseError {
// ignore
}
})
asR;
}
return result;
} finally {
if (!isAsync) {
try {
arena.releaseAll();
} onDoubleReleaseError {
// ignore
}
}
}
}
Throw on release
Add/replace an option called booleanValue(releaseWith: arena) to each built-in converter:
The arena should store references as a set of references (Set<JReference>) or Map of references to release Callbacks (Map<JReference,Function>), adding duplicate references will throw an upon registration.
Stack traces would then contain useful information.
This is similar to the syntax that the ffi uses anyway:
final pTitle ='Window title'.toNativeUtf16(allocator: arena);
The text was updated successfully, but these errors were encountered:
The
using((arena) => ...)
function provided byjni
is great for ensuring that all references are disposed at the end of the code block.There are many instances where the
arena
is not used to dispose the reference.When using:
booleanValue(releaseOriginal: true)
intValue(releaseOriginal: true)
toDartString(releaseOriginal: true)
It is quite easy to mistakenly use a
releaseBy(...)
in conjunction with one of these functions and get yourself aDoubleReleaseError
.However
using
provides no helpful information about where this 2nd release is registered.Possible Solutions
Don't throw on double release
My personal favorite
My Custom Function
Throw on release
Add/replace an option called
booleanValue(releaseWith: arena)
to each built-in converter:The arena should store references as a set of references (
Set<JReference>
) or Map of references to release Callbacks (Map<JReference,Function>
), adding duplicate references will throw an upon registration.Stack traces would then contain useful information.
This is similar to the syntax that the
ffi
uses anyway:The text was updated successfully, but these errors were encountered: