Thx Ian to pointing me to the documentation I read it, but still unsure 
regarding my example. Probably rule number 4 could apply, so let me go 
through my example and correct me if I'm wrong.

1. C.CreateApp() creates memory in C code and returns it as  C void pointer
2. Go function CreateApp() returns that as MyAppHwnd (uintptr ) so 
MyAppHwnd contains a valid C memory address
3. app variable holds still a valid C memory address as long as it is not 
deleted in C code
4. So calling ShowApp() it should be legit to convert MyAppHwnd  back to an 
unsafe.Pointer and cast it to C.MyAppPtr. It is still a valid memory 
address in C and therefore should be interpreted as pointer in C, right?

So therefore the warning can be ignored or do I miss something important?

Cheers
Sandro


Ian Lance Taylor schrieb am Freitag, 9. Juli 2021 um 05:37:16 UTC+2:

> On Thu, Jul 8, 2021 at 10:09 AM snmed <sandro....@gmail.com> wrote:
> >
> > Thanks for your reply. I came across a similar solution today, I made a 
> struct which is visible outside the package and use a private field myApp 
> C.MyAppPtr to hide the C type.
> > I still wondering why the uintptr version works but shows a warning 
> "possible misuse of unsafe.pointer", CGO documentation is not very clear to 
> me about the intricacies of type conversions between Go and C.
>
> There is a very limited number of cases in which it is OK to convert a
> pointer to uintptr. Those cases are described at
> https://golang.org/pkg/unsafe.
>
> Ian
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/72ea6248-f845-45c4-94b9-fab7027a60b2n%40googlegroups.com.

Reply via email to