-
Notifications
You must be signed in to change notification settings - Fork 18k
runtime/cgo: Go pointer checking fails if function has receiver #30894
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
Comments
Seems like the error has probably originated from runtime/cgocall.go. It's catching on the fact that the function is in the heap, which makes sense somewhat as to why that is not allowed (memory could change, etc.). What would be the recommend workaround of this, if there is one? Closing as this isn't a bug. |
If all you want to do is print a pointer value, then the workaround is to call |
Thanks for jumping in Ian. I wish the solution was that easy, but the problem I am trying to solve is passing along a pointer to a struct that contains information for a callback handler. The struct it self contains the Golang callback to invoke when the CGO code call back is invoked, this works fine if the Golang callback has a signature of For example, in the library I have written it sets up a DynamicStore for macOS that is used to receive notifications from the OS when a configuration item or state change occurs, the callback invoked by macOS is a CGO function. During the creation of the DynamicStore you can set a "context" (arbitrary pointer to some data) which is then included in the parameters of the callback when it is invoked, I've set the context to the pointer of a Golang struct. The Golang struct contains a Golang function as a variable that is called by the DynamicStore CGO callback to inform the user of the library. This is all done to provide a bit of a "cleaner" interface to the macOS libraries from Go but I am sure there is another way I can handle this, just feels like the current form is the most idiomatic. |
In the general case you need to use a |
Yeah, got the feeling that might have to be the way, was just hoping there might of been a different solution I couldn't think of. But any ways, thank you Ian. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?What did you do?
Working on a library to wrap calls to CoreFoundation and SystemConfiguration functions on macOS, part of the library setups up a callback for receiving system change notifications.
When setting up the notification if the callback method is plain function then the program operates fine, but if the function has a receiver then CGO complains the pointer contains a Go pointer.
This doesn't really make any sense to me as the type finger print is the same on both functions and it fails even if the receiver isn't a pointer (value copy).
I've managed to replicate the behavior with the following code
What did you expect to see?
I would expect the second call to pass as there is no difference in the variable type and the receiver of the function isn't a pointer.
If it is supposed to fail then why do normal functions not fail as well?
What did you see instead?
The text was updated successfully, but these errors were encountered: