Because the C function I'm calling takes a "void *" param. Can you pass a regular Go int as a "void *" argument somehow without going through unsafe.Pointer?
Note I don't directly call go_readcallback; I call op_open_callbacks (not under my control) and pass it the go_readcallback as a callback (hence the name). It does some setup, and only calls go_readcallback when data comes in. With that void * argument that it was passed during this initial call, here. On Thursday, 9 July 2020 at 20:11:24 UTC+1 Tamás Gulácsi wrote: > As far as I see, you pass c.id to go_readcallback, which gets the stream > from streamsMap. > > Why unsafe.Pointer and uintptr? Why not just a simple int or uint32 ? > For map key, they're indifferent, and the unsafe.Pointer is totally > uneeded here. > > > > 2020. július 9., csütörtök 18:58:52 UTC+2 időpontban Hraban Luyat a > következőt írta: >> >> Hi all, >> >> In package github.com/hraban/opus (currently 57179df), if I run go test >> -race I get an error: >> >> fatal error: checkptr: unsafe pointer arithmetic >> >> The offending line is: >> >> https://github.com/hraban/opus/blob/v2/stream.go#L108: >> >> oggfile := C.op_open_callbacks( >> unsafe.Pointer(s.id), // <-- this guy >> &C.callbacks, >> nil, >> 0, >> &errno) >> >> Background: I need to pass a reference to a go struct to that C call, so >> when it calls my callback function, that callback function can find the Go >> struct. The C function expects a void *, but I have a map[uintptr]myType >> locally with a fresh int for every new struct. I cast that uintptr to an >> unsafe.Pointer, pass that to the C call, and once the callback is called, I >> cast it back to a uintptr, and use that to fetch the relevant struct from >> the map. That's what the "s.id" thing is. >> >> If I understand correctly, since 1.14 (?) go test -race includes >> -d=checkptr, and checkptr does not like arbitrary uintptrs to be case to >> unsafe.Pointer. However, as far as I understand, that error is spurious in >> my specific case, because nobody ever dereferences the pointer at all, or >> does any arithmetic on it; it's only cast straight back to a uintptr. >> >> Question 1: Is my map[uintptr]myType rigmarole even necessary? It came >> about from my understanding of >> https://github.com/golang/proposal/blob/master/design/12416-cgo-pointers.md, >> but maybe I misunderstood. >> >> Question 2: if it is necessary, is this "arbitrary uintptr in void *" >> valid? As far as I understand the C standard, it is, but again maybe I >> misunderstood. >> >> Question 3: If it is valid, what can / should I do about go test -race? >> >> Related: >> https://groups.google.com/g/golang-nuts/c/FAOZoQzodAI/m/IF_xKit2DXYJ , >> seems like a similar issue, and the reply suggests the approach is valid. >> >> Thanks all and kind regards >> >> Hraban >> >> >> Full go test -race output: >> >> fatal error: checkptr: unsafe pointer arithmetic >> >> goroutine 56 [running]: >> runtime.throw(0x42354f9, 0x23) >> /usr/local/opt/go/libexec/src/runtime/panic.go:1116 +0x72 >> fp=0xc000048ce0 sp=0xc000048cb0 pc=0x4034c22 >> runtime.checkptrArithmetic(0x40, 0x0, 0x0, 0x0) >> /usr/local/opt/go/libexec/src/runtime/checkptr.go:26 +0xce >> fp=0xc000048d10 sp=0xc000048ce0 pc=0x400859e >> _/Users/hraban/code/personal/opus.(*Stream).Init.func1(0xc0001525c0, >> 0xc000128758, 0x1) >> /Users/hraban/code/personal/opus/stream.go:108 +0x6a fp=0xc000048d68 >> sp=0xc000048d10 pc=0x419ddba >> _/Users/hraban/code/personal/opus.(*Stream).Init(0xc0001525c0, 0x42630c0, >> 0xc0001323e0, 0x0, 0x0) >> /Users/hraban/code/personal/opus/stream.go:112 +0x277 fp=0xc000048e38 >> sp=0xc000048d68 pc=0x419a9c7 >> _/Users/hraban/code/personal/opus.NewStream(0x42630c0, 0xc0001323e0, 0x0, >> 0x0, 0x0) >> /Users/hraban/code/personal/opus/stream.go:75 +0xa0 fp=0xc000048e88 >> sp=0xc000048e38 pc=0x419a6b0 >> _/Users/hraban/code/personal/opus.TestStreamIllegal(0xc0002fcb40) >> /Users/hraban/code/personal/opus/stream_test.go:20 +0xc0 >> fp=0xc000048ed0 sp=0xc000048e88 pc=0x4194320 >> testing.tRunner(0xc0002fcb40, 0x4238c08) >> /usr/local/opt/go/libexec/src/testing/testing.go:991 +0x1ec >> fp=0xc000048fd0 sp=0xc000048ed0 pc=0x41270ec >> runtime.goexit() >> /usr/local/opt/go/libexec/src/runtime/asm_amd64.s:1373 +0x1 >> fp=0xc000048fd8 sp=0xc000048fd0 pc=0x4066851 >> created by testing.(*T).Run >> /usr/local/opt/go/libexec/src/testing/testing.go:1042 +0x661 >> >> goroutine 1 [chan receive]: >> testing.(*T).Run(0xc000156000, 0x4230a45, 0x11, 0x4238c08, 0x1) >> /usr/local/opt/go/libexec/src/testing/testing.go:1043 +0x699 >> testing.runTests.func1(0xc000156000) >> /usr/local/opt/go/libexec/src/testing/testing.go:1284 +0xa7 >> testing.tRunner(0xc000156000, 0xc00013bd50) >> /usr/local/opt/go/libexec/src/testing/testing.go:991 +0x1ec >> testing.runTests(0xc000132040, 0x437c680, 0x1d, 0x1d, 0x0) >> /usr/local/opt/go/libexec/src/testing/testing.go:1282 +0x528 >> testing.(*M).Run(0xc000154000, 0x0) >> /usr/local/opt/go/libexec/src/testing/testing.go:1199 +0x300 >> main.main() >> _testmain.go:100 +0x224 >> exit status 2 >> FAIL _/Users/hraban/code/personal/opus 0.231s >> > -- 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/7d8384a3-c8e2-4787-b77d-74fc13f66f35n%40googlegroups.com.