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

@ -7,14 +7,13 @@ class Submodule {
/// Lookups submodule information by [name] or path (they are usually the
/// same).
///
/// **IMPORTANT**: Should be freed to release allocated memory.
///
/// Throws a [LibGit2Error] if error occured.
Submodule.lookup({required Repository repo, required String name}) {
_submodulePointer = bindings.lookup(
repoPointer: repo.pointer,
name: name,
);
_finalizer.attach(this, _submodulePointer, detach: this);
}
/// Adds a submodule to the index.
@ -29,8 +28,6 @@ class Submodule {
/// [callbacks] is the combination of callback functions from [Callbacks]
/// object.
///
/// **IMPORTANT**: Should be freed to release allocated memory.
///
/// Throws a [LibGit2Error] if error occured.
Submodule.add({
required Repository repo,
@ -49,6 +46,8 @@ class Submodule {
bindings.clone(submodule: _submodulePointer, callbacks: callbacks);
bindings.addFinalize(_submodulePointer);
_finalizer.attach(this, _submodulePointer, detach: this);
}
/// Pointer to memory address for allocated submodule object.
@ -264,7 +263,10 @@ class Submodule {
}
/// Releases memory allocated for submodule object.
void free() => bindings.free(_submodulePointer);
void free() {
bindings.free(_submodulePointer);
_finalizer.detach(this);
}
@override
String toString() {
@ -273,3 +275,9 @@ class Submodule {
'workdirOid: $workdirOid, ignore: $ignore, updateRule: $updateRule}';
}
}
// coverage:ignore-start
final _finalizer = Finalizer<Pointer<git_submodule>>(
(pointer) => bindings.free(pointer),
);
// coverage:ignore-end