Skip to content

Nil pointer dereference in io_api example #60

@wjkoh

Description

@wjkoh

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/malgo@v0.11.23/context.go:89
github.com/gen2brain/malgo.InitDevice.func2({0x14000000120?}, 0x14000000120, 0x149014000)
	/Users/wjkoh/go/pkg/mod/github.com/gen2brain/malgo@v0.11.23/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/malgo@v0.11.23/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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions