-
Notifications
You must be signed in to change notification settings - Fork 323
[next] Prevent slot int variable from being GCed. #219
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
[next] Prevent slot int variable from being GCed. #219
Conversation
Before this change, there were no users of slot int variable in the Go world (just a pointer to it that ended up in C world only), so Go's garbage collector would free it and its value could not retrieved later (once a pointer to it comes back to Go world from C world). Keep a pointer to it in the Go world so that does not happen. Fixes #218.
Style wise, it might be nicer to do There are many, many style variations that all perform the same goal, and I don't know the most idiomatic way to solve this task, so I just went with the smallest diff PR for now. Suggestions for better style are welcome. |
Thanks for looking into this. I wonder if replacing return return value to
would work as well. IIRC in go an |
That's an interesting idea. I just gave it a try. It wouldn't work exactly as you wrote it, because you can't convert from a non-pointer type to
However, it's possible to force that conversion by going through return unsafe.Pointer(uintptr(slot)) However, using that results in a fatal error:
It would be possible to work around that by changing type of the struct git_remote_callbacks {
...
/**
* This will be passed to each of the callbacks in this struct
* as the last parameter.
*/
void *payload;
}; And func populateRemoteCallbacks(ptr *C.git_remote_callbacks, callbacks *RemoteCallbacks) {
...
ptr.payload = pointerHandles.Track(callbacks)
} Unless... Is it possible to assign a Go integer type to C's What do you think? |
I don't think it's possible (likely for good reasons, as
With that, I conclude your suggestion is not possible without changes to libgit2's internals, unless you find another place to store the Ok, it might be possible to allocate a new Let me know your thoughts before I continue forward looking for a different solution. There are many possibilities/workarounds. An alternative is to stick with the suggested solution in this PR. |
So is Go's int, but we can work around that by using a |
The issue is not the size of data, but the type. The
Can you please elaborate what you're suggesting? |
This can be undo once libgit2/git2go#218 is resolved. There is an open PR libgit2/git2go#219 that resolves it, waiting on further review and not merged yet.
But anyway, let's merge this as it solves the issue and we can figure out if we want to do something else later. |
[next] Prevent slot int variable from being GCed.
I was going to suggest doing exactly that, sounds good. Thanks for merging. |
Before this change, there were no users of
slot
int variable in the Go world (just a pointer to it that ended up in C world only), so Go's garbage collector would free it and its value could not retrieved later (once a pointer to it comes back to Go world from C world).Keep a pointer to it in the Go world so that does not happen.
Fixes #218. See that issue for a detailed analysis of the problem, and a reproducible test case (in a package that imports this one).
Please review for correctness and style, feedback is welcome.
There are many, many ways of doing the task of "keeping some reference to
slot
in Go so it's not GCed, including keeping a pointer to it as*int
, orunsafe.Pointer
, orinterface{}
. I've reused theset
map since it seemed appropriate, but a new/different map can be created instead. Let me know if you prefer a different style and I can change it.