Skip to content

Commit 0e9336b

Browse files
committed
commit: add keep-alives for those that need conversion to pointer receivers
We can't work on the copies here, we need to have pointer receivers so we know we're keeping alive the object whose finalizer would free the unmanaged memory we're working with.
1 parent 5d466ff commit 0e9336b

File tree

1 file changed

+28
-15
lines changed

1 file changed

+28
-15
lines changed

commit.go

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,19 @@ type Commit struct {
1818
cast_ptr *C.git_commit
1919
}
2020

21-
func (c Commit) Message() string {
22-
return C.GoString(C.git_commit_message(c.cast_ptr))
21+
func (c *Commit) Message() string {
22+
ret := C.GoString(C.git_commit_message(c.cast_ptr))
23+
runtime.KeepAlive(c)
24+
return ret
2325
}
2426

25-
func (c Commit) RawMessage() string {
26-
return C.GoString(C.git_commit_message_raw(c.cast_ptr))
27+
func (c *Commit) RawMessage() string {
28+
ret := C.GoString(C.git_commit_message_raw(c.cast_ptr))
29+
runtime.KeepAlive(c)
30+
return ret
2731
}
2832

29-
func (c Commit) ExtractSignature() (string, string, error) {
33+
func (c *Commit) ExtractSignature() (string, string, error) {
3034

3135
var c_signed C.git_buf
3236
defer C.git_buf_free(&c_signed)
@@ -40,7 +44,7 @@ func (c Commit) ExtractSignature() (string, string, error) {
4044
runtime.LockOSThread()
4145
defer runtime.UnlockOSThread()
4246
ret := C.git_commit_extract_signature(&c_signature, &c_signed, repo, oid.toC(), nil)
43-
47+
runtime.KeepAlive(oid)
4448
if ret < 0 {
4549
return "", "", MakeGitError(ret)
4650
} else {
@@ -49,36 +53,45 @@ func (c Commit) ExtractSignature() (string, string, error) {
4953

5054
}
5155

52-
func (c Commit) Summary() string {
53-
return C.GoString(C.git_commit_summary(c.cast_ptr))
56+
func (c *Commit) Summary() string {
57+
ret := C.GoString(C.git_commit_summary(c.cast_ptr))
58+
runtime.KeepAlive(c)
59+
return ret
5460
}
5561

56-
func (c Commit) Tree() (*Tree, error) {
62+
func (c *Commit) Tree() (*Tree, error) {
5763
var ptr *C.git_tree
5864

5965
runtime.LockOSThread()
6066
defer runtime.UnlockOSThread()
6167

6268
err := C.git_commit_tree(&ptr, c.cast_ptr)
69+
runtime.KeepAlive(c)
6370
if err < 0 {
6471
return nil, MakeGitError(err)
6572
}
6673

6774
return allocTree(ptr, c.repo), nil
6875
}
6976

70-
func (c Commit) TreeId() *Oid {
71-
return newOidFromC(C.git_commit_tree_id(c.cast_ptr))
77+
func (c *Commit) TreeId() *Oid {
78+
ret := newOidFromC(C.git_commit_tree_id(c.cast_ptr))
79+
runtime.KeepAlive(c)
80+
return c
7281
}
7382

74-
func (c Commit) Author() *Signature {
83+
func (c *Commit) Author() *Signature {
7584
cast_ptr := C.git_commit_author(c.cast_ptr)
76-
return newSignatureFromC(cast_ptr)
85+
ret := newSignatureFromC(cast_ptr)
86+
runtime.KeepAlive(c)
87+
return ret
7788
}
7889

79-
func (c Commit) Committer() *Signature {
90+
func (c *Commit) Committer() *Signature {
8091
cast_ptr := C.git_commit_committer(c.cast_ptr)
81-
return newSignatureFromC(cast_ptr)
92+
ret := newSignatureFromC(cast_ptr)
93+
runtime.KeepAlive(c)
94+
return ret
8295
}
8396

8497
func (c *Commit) Parent(n uint) *Commit {

0 commit comments

Comments
 (0)