Nil pointer dereference in `io_api` example
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 https://github.com/gen2brain/malgo/blob/d802981a69a143b335d459dd60f5a7956b0d2f35/_examples/io_api/stream.go#L10. 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?
+@egonelbre
@wjkoh That is just an example that was not updated to follow the changes that happened. If you can send a PR.