An uintptr *is not a pointer*, so 2. is false.
NOTHING at Go's side holds anything regarding MyAppHwnd, and it doesn't 
seem to be a pointer at all (just a number),
so converting that to an unsafe.Pointer is unsafe.

The rules about uintptr are there for a reason!
Only the allowed use cases are guaranteed to have the desired effect, by 
preventing any memory movement
int that block of code.

Tamas
snmed a következőt írta (2021. július 9., péntek, 8:21:37 UTC+2):

> 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/e0b9351e-83ea-4d9f-9b1c-756385c9a132n%40googlegroups.com.

Reply via email to