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,7 +6,9 @@ import 'package:libgit2dart/src/bindings/note.dart' as bindings;
class Note {
/// Initializes a new instance of the [Note] class from provided
/// pointer to note and annotatedOid objects in memory.
Note(this._notePointer, this._annotatedOidPointer);
Note(this._notePointer, this._annotatedOidPointer) {
_finalizer.attach(this, _notePointer, detach: this);
}
/// Lookups the note for an [annotatedOid].
///
@ -16,8 +18,6 @@ class Note {
///
/// [notesRef] is the canonical name of the reference to use. Defaults to "refs/notes/commits".
///
/// **IMPORTANT**: Notes must be freed to release allocated memory.
///
/// Throws a [LibGit2Error] if error occured.
Note.lookup({
required Repository repo,
@ -30,6 +30,7 @@ class Note {
notesRef: notesRef,
);
_annotatedOidPointer = annotatedOid.pointer;
_finalizer.attach(this, _notePointer, detach: this);
}
/// Pointer to memory address for allocated note object.
@ -107,8 +108,6 @@ class Note {
/// Returns list of notes for [repo]sitory.
///
/// **IMPORTANT**: Notes must be freed to release allocated memory.
///
/// Throws a [LibGit2Error] if error occured.
static List<Note> list(Repository repo) {
final notesPointers = bindings.list(repo.pointer);
@ -132,10 +131,19 @@ class Note {
Oid get annotatedOid => Oid(_annotatedOidPointer);
/// Releases memory allocated for note object.
void free() => bindings.free(_notePointer);
void free() {
bindings.free(_notePointer);
_finalizer.detach(this);
}
@override
String toString() {
return 'Note{oid: $oid, message: $message, annotatedOid: $annotatedOid}';
}
}
// coverage:ignore-start
final _finalizer = Finalizer<Pointer<git_note>>(
(pointer) => bindings.free(pointer),
);
// coverage:ignore-end