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

@ -14,20 +14,17 @@ class AnnotatedCommit {
/// It is preferable to use [AnnotatedCommit.fromReference] instead of this
/// one, for commit to contain more information about how it was looked up.
///
/// **IMPORTANT**: Should be freed to release allocated memory.
///
/// Throws a [LibGit2Error] if error occured.
AnnotatedCommit.lookup({required Repository repo, required Oid oid}) {
_annotatedCommitPointer = bindings.lookup(
repoPointer: repo.pointer,
oidPointer: oid.pointer,
);
_finalizer.attach(this, _annotatedCommitPointer, detach: this);
}
/// Creates an annotated commit from the given [reference].
///
/// **IMPORTANT**: Should be freed to release allocated memory.
///
/// Throws a [LibGit2Error] if error occured.
AnnotatedCommit.fromReference({
required Repository repo,
@ -37,6 +34,7 @@ class AnnotatedCommit {
repoPointer: repo.pointer,
referencePointer: reference.pointer,
);
_finalizer.attach(this, _annotatedCommitPointer, detach: this);
}
/// Creates an annotated commit from a revision string.
@ -44,8 +42,6 @@ class AnnotatedCommit {
/// See `man gitrevisions`, or http://git-scm.com/docs/git-rev-parse.html#_specifying_revisions
/// for information on the syntax accepted.
///
/// **IMPORTANT**: Should be freed to release allocated memory.
///
/// Throws a [LibGit2Error] if error occured.
AnnotatedCommit.fromRevSpec({
required Repository repo,
@ -55,6 +51,7 @@ class AnnotatedCommit {
repoPointer: repo.pointer,
revspec: spec,
);
_finalizer.attach(this, _annotatedCommitPointer, detach: this);
}
/// Creates an annotated commit from the given fetch head data.
@ -67,8 +64,6 @@ class AnnotatedCommit {
///
/// [oid] is the commit object id of the remote branch.
///
/// **IMPORTANT**: Should be freed to release allocated memory.
///
/// Throws a [LibGit2Error] if error occured.
AnnotatedCommit.fromFetchHead({
required Repository repo,
@ -82,6 +77,7 @@ class AnnotatedCommit {
remoteUrl: remoteUrl,
oid: oid.pointer,
);
_finalizer.attach(this, _annotatedCommitPointer, detach: this);
}
late final Pointer<git_annotated_commit> _annotatedCommitPointer;
@ -98,5 +94,14 @@ class AnnotatedCommit {
String get refName => bindings.refName(_annotatedCommitPointer);
/// Releases memory allocated for commit object.
void free() => bindings.free(_annotatedCommitPointer);
void free() {
bindings.free(_annotatedCommitPointer);
_finalizer.detach(this);
}
}
// coverage:ignore-start
final _finalizer = Finalizer<Pointer<git_annotated_commit>>(
(pointer) => bindings.free(pointer),
);
// coverage:ignore-end