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

Nil pointer dereference in io_api example #60

Open
wjkoh opened this issue Oct 23, 2024 · 2 comments
Open

Nil pointer dereference in io_api example #60

wjkoh opened this issue Oct 23, 2024 · 2 comments

Comments

@wjkoh
Copy link

wjkoh commented Oct 23, 2024

Hi @gen2brain,

I'm a big fan of the malgo package! While exploring the io_api example, I encountered an error related to a nil pointer dereference. The issue seems to stem from stream() using malgo.DefaultContext, which is initially nil. This triggers the error "invalid memory address or nil pointer dereference" when calling malgo.InitDevice() in

device, err := malgo.InitDevice(malgo.DefaultContext, deviceConfig, deviceCallbacks)
. See the full error stack trace below:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x0 pc=0x102c419d0]

goroutine 1 [running]:
github.com/gen2brain/malgo.Context.cptr(...)
	/Users/wjkoh/go/pkg/mod/github.com/gen2brain/[email protected]/context.go:89
github.com/gen2brain/malgo.InitDevice.func2({0x14000000120?}, 0x14000000120, 0x149014000)
	/Users/wjkoh/go/pkg/mod/github.com/gen2brain/[email protected]/device.go:51 +0x20
github.com/gen2brain/malgo.InitDevice({0x70?}, {0x2, 0xac44, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, ...}, ...)
	/Users/wjkoh/go/pkg/mod/github.com/gen2brain/[email protected]/device.go:51 +0x1d0
main.stream({0x102d140f8, 0x1400002e050}, 0x1400007a0e0, {0x2, 0xac44, 0x0, 0x0, 0x0, 0x0, 0x0, ...}, ...)
	/Users/wjkoh/media-over-grpc/cmd/record/stream.go:10 +0x78
main.Capture({0x102d140f8, 0x1400002e050}, {0x102d13e28, 0x14000054040}, {0x27d70?, 0x14000027d78?, 0x102cb53f8?})
	/Users/wjkoh/media-over-grpc/cmd/record/capture.go:35 +0x144
main.main.func3(0x14000027eb8, 0x14000054040, {0x602?, 0x1b6?, 0x0?})
	/Users/wjkoh/media-over-grpc/cmd/record/main.go:45 +0xb4
main.main()
	/Users/wjkoh/media-over-grpc/cmd/record/main.go:53 +0x140
exit status 2

Temporary Fix:

I was able to work around this by initializing a context and setting malgo.DefaultContext before calling io_api functions:

	ctx, err := malgo.InitContext(nil, malgo.ContextConfig{}, func(message string) {
		log.Printf("LOG <%v>\n", message)
	})
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	defer func() {
		_ = ctx.Uninit()
		ctx.Free()
	}()
	malgo.DefaultContext = ctx.Context

Question:

While this workaround solves the immediate problem, it feels a bit like a hack. Is there a recommended approach to avoid this nil pointer issue in the io_api example?

@wjkoh wjkoh changed the title Nil pointer dereference in io_api example Nil pointer dereference in io_api example Oct 23, 2024
@wjkoh
Copy link
Author

wjkoh commented Oct 23, 2024

+@egonelbre

@gen2brain
Copy link
Owner

@wjkoh That is just an example that was not updated to follow the changes that happened. If you can send a PR.

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