summaryrefslogtreecommitdiff
path: root/blob.go
diff options
context:
space:
mode:
authorCarlos Martín Nieto <[email protected]>2017-07-08 16:51:22 +0200
committerGitHub <[email protected]>2017-07-08 16:51:22 +0200
commit08db2e2c167404c5ed9be0fc1c995e41bad479bb (patch)
tree4a9c1357f3682d4134318e73ef85aa172cbfbd48 /blob.go
parent29c0b730076fe402c22ea3e3a11a7ed541663637 (diff)
parent55a1096141519a1f380d0702671cfe9bf90ec435 (diff)
Merge pull request #393 from libgit2/cmn/keepalive-all-the-things
KeepAlive all the things
Diffstat (limited to 'blob.go')
-rw-r--r--blob.go23
1 files changed, 17 insertions, 6 deletions
diff --git a/blob.go b/blob.go
index 73a4a19..5235597 100644
--- a/blob.go
+++ b/blob.go
@@ -21,13 +21,19 @@ type Blob struct {
}
func (v *Blob) Size() int64 {
- return int64(C.git_blob_rawsize(v.cast_ptr))
+ ret := int64(C.git_blob_rawsize(v.cast_ptr))
+ runtime.KeepAlive(v)
+ return ret
}
func (v *Blob) Contents() []byte {
size := C.int(C.git_blob_rawsize(v.cast_ptr))
buffer := unsafe.Pointer(C.git_blob_rawcontent(v.cast_ptr))
- return C.GoBytes(buffer, size)
+
+ goBytes := C.GoBytes(buffer, size)
+ runtime.KeepAlive(v)
+
+ return goBytes
}
func (repo *Repository) CreateBlobFromBuffer(data []byte) (*Oid, error) {
@@ -53,6 +59,7 @@ func (repo *Repository) CreateBlobFromBuffer(data []byte) (*Oid, error) {
}
ecode := C.git_blob_create_frombuffer(&id, repo.ptr, unsafe.Pointer(&data[0]), size)
+ runtime.KeepAlive(repo)
if ecode < 0 {
return nil, MakeGitError(ecode)
}
@@ -102,16 +109,18 @@ func (repo *Repository) CreateFromStream(hintPath string) (*BlobWriteStream, err
return nil, MakeGitError(ecode)
}
- return newBlobWriteStreamFromC(stream), nil
+ return newBlobWriteStreamFromC(stream, repo), nil
}
type BlobWriteStream struct {
- ptr *C.git_writestream
+ ptr *C.git_writestream
+ repo *Repository
}
-func newBlobWriteStreamFromC(ptr *C.git_writestream) *BlobWriteStream {
+func newBlobWriteStreamFromC(ptr *C.git_writestream, repo *Repository) *BlobWriteStream {
stream := &BlobWriteStream{
- ptr: ptr,
+ ptr: ptr,
+ repo: repo,
}
runtime.SetFinalizer(stream, (*BlobWriteStream).Free)
@@ -128,6 +137,7 @@ func (stream *BlobWriteStream) Write(p []byte) (int, error) {
defer runtime.UnlockOSThread()
ecode := C._go_git_writestream_write(stream.ptr, ptr, size)
+ runtime.KeepAlive(stream)
if ecode < 0 {
return 0, MakeGitError(ecode)
}
@@ -147,6 +157,7 @@ func (stream *BlobWriteStream) Commit() (*Oid, error) {
defer runtime.UnlockOSThread()
ecode := C.git_blob_create_fromstream_commit(&oid, stream.ptr)
+ runtime.KeepAlive(stream)
if ecode < 0 {
return nil, MakeGitError(ecode)
}