refactor!: use Finalizer to automatically free allocated memory for objects (#48)

BREAKING CHANGE: signature change for remote and repository callbacks during repository clone operation.
This commit is contained in:
Aleksey Kulikov 2022-04-28 11:04:48 +03:00 committed by GitHub
parent 94c40f9a94
commit a3213a88a2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
103 changed files with 2278 additions and 2595 deletions

View file

@ -6,11 +6,10 @@ import 'package:libgit2dart/src/bindings/packbuilder.dart' as bindings;
class PackBuilder {
/// Initializes a new instance of [PackBuilder] class.
///
/// **IMPORTANT**: Should be freed to release allocated memory.
///
/// Throws a [LibGit2Error] if error occured.
PackBuilder(Repository repo) {
_packbuilderPointer = bindings.init(repo.pointer);
_finalizer.attach(this, _packbuilderPointer, detach: this);
}
/// Pointer to memory address for allocated packbuilder object.
@ -108,10 +107,19 @@ class PackBuilder {
}
/// Releases memory allocated for packbuilder object.
void free() => bindings.free(_packbuilderPointer);
void free() {
bindings.free(_packbuilderPointer);
_finalizer.detach(this);
}
@override
String toString() {
return 'PackBuilder{length: $length, writtenLength: $writtenLength}';
}
}
// coverage:ignore-start
final _finalizer = Finalizer<Pointer<git_packbuilder>>(
(pointer) => bindings.free(pointer),
);
// coverage:ignore-end