Skip to content
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

Exception using UploadMesh #263

Closed
Roboadam opened this issue Jun 3, 2023 · 3 comments
Closed

Exception using UploadMesh #263

Roboadam opened this issue Jun 3, 2023 · 3 comments

Comments

@Roboadam
Copy link

Roboadam commented Jun 3, 2023

Disclamer: I am a complete novice at golang so this may be a silly issue.

I was trying recreate the models/models_mesh_generation.c from the c raylib examples and I got "runtime error: cgo argument has Go pointer to Go pointer" when calling UploadMesh.

This is a simplified example that recreates the issue:

package main

import (
	rl "github.com/gen2brain/raylib-go/raylib"
)

func main() {
	rl.InitWindow(100, 100, "Test")
	verticies := []float32{0, 0, 0, 1, 0, 2, 2, 0, 0}
	normals := []float32{0, 1, 0, 0, 1, 0, 0, 1, 0}
	textcoord := []float32{0, 0, 0.5, 1, 1, 0}
	mesh := rl.Mesh{
		TriangleCount: 1,
		VertexCount:   3,
		Vertices:      &verticies[0],
		Normals:       &normals[0],
		Texcoords:     &textcoord[0],
	}

	rl.UploadMesh(&mesh, false)
	rl.CloseWindow()
}

Produces:

Exception has occurred: panic
"runtime error: cgo argument has Go pointer to Go pointer"
Stack:
	5  0x00007ff7e433fa2a in github.com/gen2brain/raylib-go/raylib.UploadMesh.func1
	    at C:/Users/adam/go/pkg/mod/github.com/gen2brain/raylib-go/raylib@v0.0.0-20230511170620-d84e4cc82f8d/rmodels.go:645
	6  0x00007ff7e433f9a7 in github.com/gen2brain/raylib-go/raylib.UploadMesh
	    at C:/Users/adam/go/pkg/mod/github.com/gen2brain/raylib-go/raylib@v0.0.0-20230511170620-d84e4cc82f8d/rmodels.go:645
	7  0x00007ff7e43405c5 in main.main
	    at c:/Users/adam/Desktop/go/hello.go:20
@JupiterRider
Copy link
Contributor

Hey @Roboadam,

as a workaround u can set the GODEBUG environment variable like this:
GODEBUG=cgocheck=0 go run .

More information in the go docs: https://pkg.go.dev/cmd/cgo#hdr-Passing_pointers

@JupiterRider
Copy link
Contributor

Here is a "running" example:

package main

import (
	rl "github.com/gen2brain/raylib-go/raylib"
)

func main() {
	rl.InitWindow(800, 450, "Test")
	defer rl.CloseWindow()

	verticies := []float32{0, 0, 0, 1, 0, 2, 2, 0, 0}
	normals := []float32{0, 1, 0, 0, 1, 0, 0, 1, 0}
	textcoord := []float32{0, 0, 0.5, 1, 1, 0}

	mesh := rl.Mesh{
		TriangleCount: 1,
		VertexCount:   3,
		Vertices:      &verticies[0],
		Normals:       &normals[0],
		Texcoords:     &textcoord[0],
	}

	rl.UploadMesh(&mesh, false)

	model := rl.LoadModelFromMesh(mesh)
	defer rl.UnloadModel(model)

	camera := rl.Camera3D{Position: rl.Vector3{X: 5, Y: 5, Z: 5}, Up: rl.Vector3{Y: 1}, Fovy: 45}

	for !rl.WindowShouldClose() {
		rl.UpdateCamera(&camera, rl.CameraOrbital)

		rl.BeginDrawing()
		rl.ClearBackground(rl.RayWhite)

		rl.BeginMode3D(camera)
		rl.DrawModel(model, rl.Vector3{}, 1, rl.Red)
		rl.DrawGrid(10, 1)
		rl.EndMode3D()

		rl.EndDrawing()
	}
}

Screenshot_20230603_115706

But in the end, it is going to crash:

INFO: Initializing raylib 4.5
INFO: Supported raylib modules:
INFO:     > rcore:..... loaded (mandatory)
INFO:     > rlgl:...... loaded (mandatory)
INFO:     > rshapes:... loaded (optional)
INFO:     > rtextures:. loaded (optional)
INFO:     > rtext:..... loaded (optional)
INFO:     > rmodels:... loaded (optional)
INFO:     > raudio:.... loaded (optional)
INFO: DISPLAY: Device initialized successfully
INFO:     > Display size: 1920 x 1080
INFO:     > Screen size:  800 x 450
INFO:     > Render size:  800 x 450
INFO:     > Viewport offsets: 0, 0
INFO: GLAD: OpenGL extensions loaded successfully
INFO: GL: Supported extensions count: 229
INFO: GL: OpenGL device information:
INFO:     > Vendor:   AMD
INFO:     > Renderer: AMD Radeon R9 390 Series (hawaii, LLVM 15.0.7, DRM 3.52, 6.3.5-zen1-1-zen)
INFO:     > Version:  4.6 (Core Profile) Mesa 23.1.1
INFO:     > GLSL:     4.60
INFO: GL: VAO extension detected, VAO functions loaded successfully
INFO: GL: NPOT textures extension detected, full NPOT textures supported
INFO: GL: DXT compressed textures supported
INFO: GL: ETC2/EAC compressed textures supported
INFO: TEXTURE: [ID 1] Texture loaded successfully (1x1 | R8G8B8A8 | 1 mipmaps)
INFO: TEXTURE: [ID 1] Default texture loaded successfully
INFO: SHADER: [ID 1] Vertex shader compiled successfully
INFO: SHADER: [ID 2] Fragment shader compiled successfully
INFO: SHADER: [ID 3] Program shader loaded successfully
INFO: SHADER: [ID 3] Default shader loaded successfully
INFO: RLGL: Render batch vertex buffers loaded successfully in RAM (CPU)
INFO: RLGL: Render batch vertex buffers loaded successfully in VRAM (GPU)
INFO: RLGL: Default OpenGL state initialized successfully
INFO: TEXTURE: [ID 2] Texture loaded successfully (128x128 | GRAY_ALPHA | 1 mipmaps)
INFO: FONT: Default font loaded successfully (224 glyphs)
INFO: VAO: [ID 2] Mesh uploaded successfully to VRAM (GPU)
INFO: VAO: [ID 2] Unloaded vertex array data from VRAM (GPU)
free(): invalid size
SIGABRT: abort
PC=0x7f81ada4526c m=0 sigcode=18446744073709551610
signal arrived during cgo execution

goroutine 1 [syscall, locked to thread]:
runtime.cgocall(0x4c5700, 0xc000053a98)
        /usr/lib/go/src/runtime/cgocall.go:157 +0x5c fp=0xc000053a70 sp=0xc000053a38 pc=0x44203c
github.com/gen2brain/raylib-go/raylib._Cfunc_UnloadModel({{0x3f800000, 0x0, 0x0, 0x0, 0x0, 0x3f800000, 0x0, 0x0, 0x0, 0x0, ...}, ...})
        _cgo_gotypes.go:6070 +0x45 fp=0xc000053a98 sp=0xc000053a70 pc=0x4a7825
github.com/gen2brain/raylib-go/raylib.UnloadModel.func1(0xc0000081a0?)
        /tmp/raylib-go/raylib/rmodels.go:244 +0xb8 fp=0xc000053b98 sp=0xc000053a98 pc=0x4a7fd8
github.com/gen2brain/raylib-go/raylib.UnloadModel({{0x3f800000, 0x0, 0x0, 0x0, 0x0, 0x3f800000, 0x0, 0x0, 0x0, 0x0, ...}, ...})
        /tmp/raylib-go/raylib/rmodels.go:244 +0x1e fp=0xc000053bb0 sp=0xc000053b98 pc=0x4a7efe
main.main.func1()
        /tmp/demo/main.go:26 +0x58 fp=0xc000053c38 sp=0xc000053bb0 pc=0x4a93f8
main.main()
        /tmp/demo/main.go:43 +0x529 fp=0xc000053f80 sp=0xc000053c38 pc=0x4a9349
runtime.main()
        /usr/lib/go/src/runtime/proc.go:250 +0x207 fp=0xc000053fe0 sp=0xc000053f80 pc=0x471067
runtime.goexit()
        /usr/lib/go/src/runtime/asm_amd64.s:1598 +0x1 fp=0xc000053fe8 sp=0xc000053fe0 pc=0x49a121

goroutine 2 [force gc (idle)]:
runtime.gopark(0x0?, 0x0?, 0x0?, 0x0?, 0x0?)
        /usr/lib/go/src/runtime/proc.go:381 +0xd6 fp=0xc000040fb0 sp=0xc000040f90 pc=0x471496
runtime.goparkunlock(...)
        /usr/lib/go/src/runtime/proc.go:387
runtime.forcegchelper()
        /usr/lib/go/src/runtime/proc.go:305 +0xb0 fp=0xc000040fe0 sp=0xc000040fb0 pc=0x4712d0
runtime.goexit()
        /usr/lib/go/src/runtime/asm_amd64.s:1598 +0x1 fp=0xc000040fe8 sp=0xc000040fe0 pc=0x49a121
created by runtime.init.6
        /usr/lib/go/src/runtime/proc.go:293 +0x25

goroutine 3 [GC sweep wait]:
runtime.gopark(0x0?, 0x0?, 0x0?, 0x0?, 0x0?)
        /usr/lib/go/src/runtime/proc.go:381 +0xd6 fp=0xc000041780 sp=0xc000041760 pc=0x471496
runtime.goparkunlock(...)
        /usr/lib/go/src/runtime/proc.go:387
runtime.bgsweep(0x0?)
        /usr/lib/go/src/runtime/mgcsweep.go:278 +0x8e fp=0xc0000417c8 sp=0xc000041780 pc=0x45e4ee
runtime.gcenable.func1()
        /usr/lib/go/src/runtime/mgc.go:178 +0x26 fp=0xc0000417e0 sp=0xc0000417c8 pc=0x4539a6
runtime.goexit()
        /usr/lib/go/src/runtime/asm_amd64.s:1598 +0x1 fp=0xc0000417e8 sp=0xc0000417e0 pc=0x49a121
created by runtime.gcenable
        /usr/lib/go/src/runtime/mgc.go:178 +0x6b

goroutine 4 [GC scavenge wait]:
runtime.gopark(0xc000066000?, 0x5e27b8?, 0x1?, 0x0?, 0x0?)
        /usr/lib/go/src/runtime/proc.go:381 +0xd6 fp=0xc000041f70 sp=0xc000041f50 pc=0x471496
runtime.goparkunlock(...)
        /usr/lib/go/src/runtime/proc.go:387
runtime.(*scavengerState).park(0x6c1de0)
        /usr/lib/go/src/runtime/mgcscavenge.go:400 +0x53 fp=0xc000041fa0 sp=0xc000041f70 pc=0x45c413
runtime.bgscavenge(0x0?)
        /usr/lib/go/src/runtime/mgcscavenge.go:628 +0x45 fp=0xc000041fc8 sp=0xc000041fa0 pc=0x45c9e5
runtime.gcenable.func2()
        /usr/lib/go/src/runtime/mgc.go:179 +0x26 fp=0xc000041fe0 sp=0xc000041fc8 pc=0x453946
runtime.goexit()
        /usr/lib/go/src/runtime/asm_amd64.s:1598 +0x1 fp=0xc000041fe8 sp=0xc000041fe0 pc=0x49a121
created by runtime.gcenable
        /usr/lib/go/src/runtime/mgc.go:179 +0xaa

goroutine 5 [finalizer wait]:
runtime.gopark(0x1a0?, 0x6c2220?, 0x60?, 0x98?, 0xc000040770?)
        /usr/lib/go/src/runtime/proc.go:381 +0xd6 fp=0xc000040628 sp=0xc000040608 pc=0x471496
runtime.runfinq()
        /usr/lib/go/src/runtime/mfinal.go:193 +0x107 fp=0xc0000407e0 sp=0xc000040628 pc=0x4529e7
runtime.goexit()
        /usr/lib/go/src/runtime/asm_amd64.s:1598 +0x1 fp=0xc0000407e8 sp=0xc0000407e0 pc=0x49a121
created by runtime.createfing
        /usr/lib/go/src/runtime/mfinal.go:163 +0x45

rax    0x0
rbx    0x595d
rcx    0x7f81ada4526c
rdx    0x6
rdi    0x595d
rsi    0x595d
rbp    0x7f81ad896b80
rsp    0x7fff9c28db10
r8     0xffffffff
r9     0x0
r10    0x8
r11    0x246
r12    0x7f8184497000
r13    0x6
r14    0x1000
r15    0x7f81adb59517
rip    0x7f81ada4526c
rflags 0x246
cs     0x33
fs     0x0
gs     0x0
exit status 2

This is because rl.UnloadModel is trying to wipe the slices which are controlled by the golang garbage collector.
So you have to create the slices/arrays in C or use the premade functions like rl.GenMeshPlane

@Roboadam
Copy link
Author

Roboadam commented Jun 4, 2023

Thank you so much for the thorough reply. Setting cgocheck=0 "fixes" it.

That makes perfect sense about UnloadModel and the GC not playing nicely together.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants