Add a way to cleanly shut down the library (#578)

This change adds the Shutdown() method, so that the library can be
cleanly shut down. This helps significanly reduce the amount of noise in
the leak detector.
This commit is contained in:
lhchavez 2020-06-21 06:44:06 -07:00 committed by GitHub
parent 9eaf4fed5f
commit 619a9c236b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 0 deletions

10
git.go
View File

@ -139,6 +139,16 @@ func init() {
C.git_openssl_set_locking() C.git_openssl_set_locking()
} }
// Shutdown frees all the resources acquired by libgit2. Make sure no
// references to any git2go objects are live before calling this.
// After this is called, invoking any function from this library will result in
// undefined behavior, so make sure this is called carefully.
func Shutdown() {
pointerHandles.Clear()
C.git_libgit2_shutdown()
}
// Oid represents the id for a Git object. // Oid represents the id for a Git object.
type Oid [20]byte type Oid [20]byte

View File

@ -43,6 +43,16 @@ func (v *HandleList) Untrack(handle unsafe.Pointer) {
v.Unlock() v.Unlock()
} }
// Clear stops tracking all the managed pointers.
func (v *HandleList) Clear() {
v.Lock()
for handle := range v.handles {
delete(v.handles, handle)
C.free(handle)
}
v.Unlock()
}
// Get retrieves the pointer from the given handle // Get retrieves the pointer from the given handle
func (v *HandleList) Get(handle unsafe.Pointer) interface{} { func (v *HandleList) Get(handle unsafe.Pointer) interface{} {
v.RLock() v.RLock()