From 8a73c75f1a68f2855d03e6d2ce45c95c414aa71a Mon Sep 17 00:00:00 2001 From: Carlos Martín Nieto Date: Mon, 26 May 2014 09:28:07 +0200 Subject: Keep a pointer to the repository in the objects and references Otherwise, the garbage collector might decide it's a good idea to throw away the repository instance while the C object still has a pointer to it. Hilarity ensues. --- object.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'object.go') diff --git a/object.go b/object.go index 9241ae2..10f5f7c 100644 --- a/object.go +++ b/object.go @@ -27,6 +27,7 @@ type Object interface { type gitObject struct { ptr *C.git_object + repo *Repository } func (t ObjectType) String() (string) { @@ -69,12 +70,16 @@ func (o *gitObject) Free() { C.git_object_free(o.ptr) } -func allocObject(cobj *C.git_object) Object { +func allocObject(cobj *C.git_object, repo *Repository) Object { + obj := gitObject{ + ptr: cobj, + repo: repo, + } switch ObjectType(C.git_object_type(cobj)) { case ObjectCommit: commit := &Commit{ - gitObject: gitObject{cobj}, + gitObject: obj, cast_ptr: (*C.git_commit)(cobj), } runtime.SetFinalizer(commit, (*Commit).Free) @@ -82,7 +87,7 @@ func allocObject(cobj *C.git_object) Object { case ObjectTree: tree := &Tree{ - gitObject: gitObject{cobj}, + gitObject: obj, cast_ptr: (*C.git_tree)(cobj), } runtime.SetFinalizer(tree, (*Tree).Free) @@ -90,7 +95,7 @@ func allocObject(cobj *C.git_object) Object { case ObjectBlob: blob := &Blob{ - gitObject: gitObject{cobj}, + gitObject: obj, cast_ptr: (*C.git_blob)(cobj), } runtime.SetFinalizer(blob, (*Blob).Free) -- cgit v1.2.3