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

@ -9,21 +9,19 @@ class Mailmap {
///
/// This object is empty, so you'll have to add a mailmap file before you can
/// do anything with it.
///
/// **IMPORTANT**: Should be freed to release allocated memory.
Mailmap.empty() {
libgit2.git_libgit2_init();
_mailmapPointer = bindings.init();
_finalizer.attach(this, _mailmapPointer, detach: this);
}
/// Initializes a new instance of [Mailmap] class from provided buffer.
///
/// **IMPORTANT**: Should be freed to release allocated memory.
Mailmap.fromBuffer(String buffer) {
libgit2.git_libgit2_init();
_mailmapPointer = bindings.fromBuffer(buffer);
_finalizer.attach(this, _mailmapPointer, detach: this);
}
/// Initializes a new instance of [Mailmap] class from a [repo]sitory, loading
@ -34,14 +32,13 @@ class Mailmap {
/// 1. `.mailmap` in the root of the repository's working directory, if
/// present.
/// 2. The blob object identified by the `mailmap.blob` config entry, if set.
/// NOTE: `mailmap.blob` defaults to `HEAD:.mailmap` in bare repositories
/// NOTE: `mailmap.blob` defaults to `HEAD:.mailmap` in bare repositories.
/// 3. The path in the `mailmap.file` config entry, if set.
///
/// **IMPORTANT**: Should be freed to release allocated memory.
///
/// Throws a [LibGit2Error] if error occured.
Mailmap.fromRepository(Repository repo) {
_mailmapPointer = bindings.fromRepository(repo.pointer);
_finalizer.attach(this, _mailmapPointer, detach: this);
}
/// Pointer to memory address for allocated mailmap object.
@ -94,5 +91,14 @@ class Mailmap {
}
/// Releases memory allocated for mailmap object.
void free() => bindings.free(_mailmapPointer);
void free() {
bindings.free(_mailmapPointer);
_finalizer.detach(this);
}
}
// coverage:ignore-start
final _finalizer = Finalizer<Pointer<git_mailmap>>(
(pointer) => bindings.free(pointer),
);
// coverage:ignore-end