Fatal error: invalid pointer found on stack

Hi there
I’ve just found the strangest bug I’ve seen until now.
I’m working on cimgui-go library (code here: GitHub - gucio321/cimgui-go at callbacks)

I’m getting this panic:

runtime: bad pointer in frame runtime.assertE2I2 at 0xc000145408: 0x1
fatal error: invalid pointer found on stack

runtime stack:
runtime.throw({0x859d38?, 0x9f1c80?})
	/usr/local/go/src/runtime/panic.go:1077 +0x5c fp=0x7ffffa0eabe0 sp=0x7ffffa0eabb0 pc=0x43845c
runtime.adjustpointers(0x7ffffa0eae58?, 0x7ffffa0eaca0, 0x459e45?, {0x7ffffa0eae58?, 0x0?})
	/usr/local/go/src/runtime/stack.go:627 +0x1ad fp=0x7ffffa0eac40 sp=0x7ffffa0eabe0 pc=0x44feed
runtime.adjustframe(0x7ffffa0eae58, 0x7ffffa0ead38)
	/usr/local/go/src/runtime/stack.go:684 +0xdb fp=0x7ffffa0eacd0 sp=0x7ffffa0eac40 pc=0x45001b
runtime.copystack(0xc0000061a0, 0x800000002?)
	/usr/local/go/src/runtime/stack.go:935 +0x2c5 fp=0x7ffffa0eafc8 sp=0x7ffffa0eacd0 pc=0x4507c5
runtime.newstack()
	/usr/local/go/src/runtime/stack.go:1116 +0x47f fp=0x7ffffa0eb178 sp=0x7ffffa0eafc8 pc=0x450d7f
traceback: unexpected SPWRITE function runtime.morestack
runtime.morestack()
	/usr/local/go/src/runtime/asm_amd64.s:593 +0x8f fp=0x7ffffa0eb180 sp=0x7ffffa0eb178 pc=0x4636ef

This is caused by the following line

        v := texture.ID()
        fmt.Printf("%v\n", v) // <- this line panics

ID is a getter func that returns a unsafe-Pointer-type field of Texture struct. This unsafe.Pointer is generated by C code.

The strangest part is, that if I modify this code in the following way:

        fmt.Println(texture)
        v := texture.ID()
        fmt.Printf("%v\n", v)

everything works as expected.

I’d be greatful for any help :wink: Is it a bug for go?
thanx in advance!

Hi @gucio321, I’m not sure at 100%, but the problem here is the fact that %v use the String() method from the Stringer implementation for the specific object (a pointer to texture) which is implemented in C in this case, relying on cgo interfacing. So, calling on texture Println before gives Golang the info about it before using it again later. Is a matter of default representation format :slight_smile: I think.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.