Skip to content

Commit 08db2e2

Browse files
authored
Merge pull request libgit2#393 from libgit2/cmn/keepalive-all-the-things
KeepAlive all the things
2 parents 29c0b73 + 55a1096 commit 08db2e2

30 files changed

+609
-194
lines changed

blame.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ func (v *Repository) BlameFile(path string, opts *BlameOptions) (*Blame, error)
7676
defer runtime.UnlockOSThread()
7777

7878
ecode := C.git_blame_file(&blamePtr, v.ptr, cpath, copts)
79+
runtime.KeepAlive(v)
7980
if ecode < 0 {
8081
return nil, MakeGitError(ecode)
8182
}
@@ -88,11 +89,15 @@ type Blame struct {
8889
}
8990

9091
func (blame *Blame) HunkCount() int {
91-
return int(C.git_blame_get_hunk_count(blame.ptr))
92+
ret := int(C.git_blame_get_hunk_count(blame.ptr))
93+
runtime.KeepAlive(blame)
94+
95+
return ret
9296
}
9397

9498
func (blame *Blame) HunkByIndex(index int) (BlameHunk, error) {
9599
ptr := C.git_blame_get_hunk_byindex(blame.ptr, C.uint32_t(index))
100+
runtime.KeepAlive(blame)
96101
if ptr == nil {
97102
return BlameHunk{}, ErrInvalid
98103
}
@@ -101,6 +106,7 @@ func (blame *Blame) HunkByIndex(index int) (BlameHunk, error) {
101106

102107
func (blame *Blame) HunkByLine(lineno int) (BlameHunk, error) {
103108
ptr := C.git_blame_get_hunk_byline(blame.ptr, C.size_t(lineno))
109+
runtime.KeepAlive(blame)
104110
if ptr == nil {
105111
return BlameHunk{}, ErrInvalid
106112
}

blob.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,19 @@ type Blob struct {
2121
}
2222

2323
func (v *Blob) Size() int64 {
24-
return int64(C.git_blob_rawsize(v.cast_ptr))
24+
ret := int64(C.git_blob_rawsize(v.cast_ptr))
25+
runtime.KeepAlive(v)
26+
return ret
2527
}
2628

2729
func (v *Blob) Contents() []byte {
2830
size := C.int(C.git_blob_rawsize(v.cast_ptr))
2931
buffer := unsafe.Pointer(C.git_blob_rawcontent(v.cast_ptr))
30-
return C.GoBytes(buffer, size)
32+
33+
goBytes := C.GoBytes(buffer, size)
34+
runtime.KeepAlive(v)
35+
36+
return goBytes
3137
}
3238

3339
func (repo *Repository) CreateBlobFromBuffer(data []byte) (*Oid, error) {
@@ -53,6 +59,7 @@ func (repo *Repository) CreateBlobFromBuffer(data []byte) (*Oid, error) {
5359
}
5460

5561
ecode := C.git_blob_create_frombuffer(&id, repo.ptr, unsafe.Pointer(&data[0]), size)
62+
runtime.KeepAlive(repo)
5663
if ecode < 0 {
5764
return nil, MakeGitError(ecode)
5865
}
@@ -102,16 +109,18 @@ func (repo *Repository) CreateFromStream(hintPath string) (*BlobWriteStream, err
102109
return nil, MakeGitError(ecode)
103110
}
104111

105-
return newBlobWriteStreamFromC(stream), nil
112+
return newBlobWriteStreamFromC(stream, repo), nil
106113
}
107114

108115
type BlobWriteStream struct {
109-
ptr *C.git_writestream
116+
ptr *C.git_writestream
117+
repo *Repository
110118
}
111119

112-
func newBlobWriteStreamFromC(ptr *C.git_writestream) *BlobWriteStream {
120+
func newBlobWriteStreamFromC(ptr *C.git_writestream, repo *Repository) *BlobWriteStream {
113121
stream := &BlobWriteStream{
114-
ptr: ptr,
122+
ptr: ptr,
123+
repo: repo,
115124
}
116125

117126
runtime.SetFinalizer(stream, (*BlobWriteStream).Free)
@@ -128,6 +137,7 @@ func (stream *BlobWriteStream) Write(p []byte) (int, error) {
128137
defer runtime.UnlockOSThread()
129138

130139
ecode := C._go_git_writestream_write(stream.ptr, ptr, size)
140+
runtime.KeepAlive(stream)
131141
if ecode < 0 {
132142
return 0, MakeGitError(ecode)
133143
}
@@ -147,6 +157,7 @@ func (stream *BlobWriteStream) Commit() (*Oid, error) {
147157
defer runtime.UnlockOSThread()
148158

149159
ecode := C.git_blob_create_fromstream_commit(&oid, stream.ptr)
160+
runtime.KeepAlive(stream)
150161
if ecode < 0 {
151162
return nil, MakeGitError(ecode)
152163
}

branch.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ func (repo *Repository) NewBranchIterator(flags BranchType) (*BranchIterator, er
8888
defer runtime.UnlockOSThread()
8989

9090
ecode := C.git_branch_iterator_new(&ptr, repo.ptr, refType)
91+
runtime.KeepAlive(repo)
9192
if ecode < 0 {
9293
return nil, MakeGitError(ecode)
9394
}
@@ -106,6 +107,8 @@ func (repo *Repository) CreateBranch(branchName string, target *Commit, force bo
106107
defer runtime.UnlockOSThread()
107108

108109
ret := C.git_branch_create(&ptr, repo.ptr, cBranchName, target.cast_ptr, cForce)
110+
runtime.KeepAlive(repo)
111+
runtime.KeepAlive(target)
109112
if ret < 0 {
110113
return nil, MakeGitError(ret)
111114
}
@@ -117,6 +120,7 @@ func (b *Branch) Delete() error {
117120
runtime.LockOSThread()
118121
defer runtime.UnlockOSThread()
119122
ret := C.git_branch_delete(b.Reference.ptr)
123+
runtime.KeepAlive(b.Reference)
120124
if ret < 0 {
121125
return MakeGitError(ret)
122126
}
@@ -133,6 +137,7 @@ func (b *Branch) Move(newBranchName string, force bool) (*Branch, error) {
133137
defer runtime.UnlockOSThread()
134138

135139
ret := C.git_branch_move(&ptr, b.Reference.ptr, cNewBranchName, cForce)
140+
runtime.KeepAlive(b.Reference)
136141
if ret < 0 {
137142
return nil, MakeGitError(ret)
138143
}
@@ -145,6 +150,7 @@ func (b *Branch) IsHead() (bool, error) {
145150
defer runtime.UnlockOSThread()
146151

147152
ret := C.git_branch_is_head(b.Reference.ptr)
153+
runtime.KeepAlive(b.Reference)
148154
switch ret {
149155
case 1:
150156
return true, nil
@@ -165,6 +171,7 @@ func (repo *Repository) LookupBranch(branchName string, bt BranchType) (*Branch,
165171
defer runtime.UnlockOSThread()
166172

167173
ret := C.git_branch_lookup(&ptr, repo.ptr, cName, C.git_branch_t(bt))
174+
runtime.KeepAlive(repo)
168175
if ret < 0 {
169176
return nil, MakeGitError(ret)
170177
}
@@ -179,6 +186,7 @@ func (b *Branch) Name() (string, error) {
179186
defer runtime.UnlockOSThread()
180187

181188
ret := C.git_branch_name(&cName, b.Reference.ptr)
189+
runtime.KeepAlive(b.Reference)
182190
if ret < 0 {
183191
return "", MakeGitError(ret)
184192
}
@@ -196,6 +204,7 @@ func (repo *Repository) RemoteName(canonicalBranchName string) (string, error) {
196204
defer runtime.UnlockOSThread()
197205

198206
ret := C.git_branch_remote_name(&nameBuf, repo.ptr, cName)
207+
runtime.KeepAlive(repo)
199208
if ret < 0 {
200209
return "", MakeGitError(ret)
201210
}
@@ -212,6 +221,7 @@ func (b *Branch) SetUpstream(upstreamName string) error {
212221
defer runtime.UnlockOSThread()
213222

214223
ret := C.git_branch_set_upstream(b.Reference.ptr, cName)
224+
runtime.KeepAlive(b.Reference)
215225
if ret < 0 {
216226
return MakeGitError(ret)
217227
}
@@ -225,6 +235,7 @@ func (b *Branch) Upstream() (*Reference, error) {
225235
defer runtime.UnlockOSThread()
226236

227237
ret := C.git_branch_upstream(&ptr, b.Reference.ptr)
238+
runtime.KeepAlive(b.Reference)
228239
if ret < 0 {
229240
return nil, MakeGitError(ret)
230241
}
@@ -241,6 +252,7 @@ func (repo *Repository) UpstreamName(canonicalBranchName string) (string, error)
241252
defer runtime.UnlockOSThread()
242253

243254
ret := C.git_branch_upstream_name(&nameBuf, repo.ptr, cName)
255+
runtime.KeepAlive(repo)
244256
if ret < 0 {
245257
return "", MakeGitError(ret)
246258
}

checkout.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ func (v *Repository) CheckoutHead(opts *CheckoutOpts) error {
188188
defer freeCheckoutOpts(cOpts)
189189

190190
ret := C.git_checkout_head(v.ptr, cOpts)
191+
runtime.KeepAlive(v)
191192
if ret < 0 {
192193
return MakeGitError(ret)
193194
}
@@ -211,6 +212,7 @@ func (v *Repository) CheckoutIndex(index *Index, opts *CheckoutOpts) error {
211212
defer freeCheckoutOpts(cOpts)
212213

213214
ret := C.git_checkout_index(v.ptr, iptr, cOpts)
215+
runtime.KeepAlive(v)
214216
if ret < 0 {
215217
return MakeGitError(ret)
216218
}
@@ -226,6 +228,7 @@ func (v *Repository) CheckoutTree(tree *Tree, opts *CheckoutOpts) error {
226228
defer freeCheckoutOpts(cOpts)
227229

228230
ret := C.git_checkout_tree(v.ptr, tree.ptr, cOpts)
231+
runtime.KeepAlive(v)
229232
if ret < 0 {
230233
return MakeGitError(ret)
231234
}

cherrypick.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ func (v *Repository) Cherrypick(commit *Commit, opts CherrypickOptions) error {
6666
defer freeCherrypickOpts(cOpts)
6767

6868
ecode := C.git_cherrypick(v.ptr, commit.cast_ptr, cOpts)
69+
runtime.KeepAlive(v)
70+
runtime.KeepAlive(commit)
6971
if ecode < 0 {
7072
return MakeGitError(ecode)
7173
}

commit.go

Lines changed: 45 additions & 21 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)
@@ -35,48 +39,59 @@ func (c Commit) ExtractSignature() (string, string, error) {
3539
defer C.git_buf_free(&c_signature)
3640

3741
oid := c.Id()
38-
3942
repo := C.git_commit_owner(c.cast_ptr)
43+
44+
runtime.LockOSThread()
45+
defer runtime.UnlockOSThread()
4046
ret := C.git_commit_extract_signature(&c_signature, &c_signed, repo, oid.toC(), nil)
41-
47+
runtime.KeepAlive(oid)
4248
if ret < 0 {
43-
return "", "", MakeGitError(ret)
49+
return "", "", MakeGitError(ret)
4450
} else {
4551
return C.GoString(c_signature.ptr), C.GoString(c_signed.ptr), nil
4652
}
47-
53+
4854
}
4955

50-
func (c Commit) Summary() string {
51-
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
5260
}
5361

54-
func (c Commit) Tree() (*Tree, error) {
62+
func (c *Commit) Tree() (*Tree, error) {
5563
var ptr *C.git_tree
5664

5765
runtime.LockOSThread()
5866
defer runtime.UnlockOSThread()
5967

6068
err := C.git_commit_tree(&ptr, c.cast_ptr)
69+
runtime.KeepAlive(c)
6170
if err < 0 {
6271
return nil, MakeGitError(err)
6372
}
6473

6574
return allocTree(ptr, c.repo), nil
6675
}
6776

68-
func (c Commit) TreeId() *Oid {
69-
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 ret
7081
}
7182

72-
func (c Commit) Author() *Signature {
83+
func (c *Commit) Author() *Signature {
7384
cast_ptr := C.git_commit_author(c.cast_ptr)
74-
return newSignatureFromC(cast_ptr)
85+
ret := newSignatureFromC(cast_ptr)
86+
runtime.KeepAlive(c)
87+
return ret
7588
}
7689

77-
func (c Commit) Committer() *Signature {
90+
func (c *Commit) Committer() *Signature {
7891
cast_ptr := C.git_commit_committer(c.cast_ptr)
79-
return newSignatureFromC(cast_ptr)
92+
ret := newSignatureFromC(cast_ptr)
93+
runtime.KeepAlive(c)
94+
return ret
8095
}
8196

8297
func (c *Commit) Parent(n uint) *Commit {
@@ -86,15 +101,21 @@ func (c *Commit) Parent(n uint) *Commit {
86101
return nil
87102
}
88103

89-
return allocCommit(cobj, c.repo)
104+
parent := allocCommit(cobj, c.repo)
105+
runtime.KeepAlive(c)
106+
return parent
90107
}
91108

92109
func (c *Commit) ParentId(n uint) *Oid {
93-
return newOidFromC(C.git_commit_parent_id(c.cast_ptr, C.uint(n)))
110+
ret := newOidFromC(C.git_commit_parent_id(c.cast_ptr, C.uint(n)))
111+
runtime.KeepAlive(c)
112+
return ret
94113
}
95114

96115
func (c *Commit) ParentCount() uint {
97-
return uint(C.git_commit_parentcount(c.cast_ptr))
116+
ret := uint(C.git_commit_parentcount(c.cast_ptr))
117+
runtime.KeepAlive(c)
118+
return ret
98119
}
99120

100121
func (c *Commit) Amend(refname string, author, committer *Signature, message string, tree *Tree) (*Oid, error) {
@@ -127,6 +148,9 @@ func (c *Commit) Amend(refname string, author, committer *Signature, message str
127148
oid := new(Oid)
128149

129150
cerr := C.git_commit_amend(oid.toC(), c.cast_ptr, cref, authorSig, committerSig, nil, cmsg, tree.cast_ptr)
151+
runtime.KeepAlive(oid)
152+
runtime.KeepAlive(c)
153+
runtime.KeepAlive(tree)
130154
if cerr < 0 {
131155
return nil, MakeGitError(cerr)
132156
}

0 commit comments

Comments
 (0)