Skip to content

Add a way to cleanly shut down the library #578

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions git.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@ func init() {
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.
type Oid [20]byte

Expand Down
10 changes: 10 additions & 0 deletions handles.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ func (v *HandleList) Untrack(handle unsafe.Pointer) {
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
func (v *HandleList) Get(handle unsafe.Pointer) interface{} {
v.RLock()
Expand Down