Skip to content

Commit bd90360

Browse files
committed
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. (cherry picked from commit 619a9c2)
1 parent f403926 commit bd90360

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

git.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,16 @@ func init() {
139139
C.git_openssl_set_locking()
140140
}
141141

142+
// Shutdown frees all the resources acquired by libgit2. Make sure no
143+
// references to any git2go objects are live before calling this.
144+
// After this is called, invoking any function from this library will result in
145+
// undefined behavior, so make sure this is called carefully.
146+
func Shutdown() {
147+
pointerHandles.Clear()
148+
149+
C.git_libgit2_shutdown()
150+
}
151+
142152
// Oid represents the id for a Git object.
143153
type Oid [20]byte
144154

handles.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ func (v *HandleList) Untrack(handle unsafe.Pointer) {
4343
v.Unlock()
4444
}
4545

46+
// Clear stops tracking all the managed pointers.
47+
func (v *HandleList) Clear() {
48+
v.Lock()
49+
for handle := range v.handles {
50+
delete(v.handles, handle)
51+
C.free(handle)
52+
}
53+
v.Unlock()
54+
}
55+
4656
// Get retrieves the pointer from the given handle
4757
func (v *HandleList) Get(handle unsafe.Pointer) interface{} {
4858
v.RLock()

0 commit comments

Comments
 (0)