I've been trying to write a simple program to play a sine wave for specific duration. I tried to use http://libsound.io and a Nim wrapper for it: https://github.com/ul/soundio.
I wrote the program in C first, which can be found here: https://gitlab.com/snippets/1883765. This is closely based off the following example provided with libsoundio: http://libsound.io/doc-2.0.0/sio_sine_8c-example.html.
Next I tried to write it in Nim, which can be found here: https://gitlab.com/snippets/1883764. I tried to make it as similar as possible to the C version. However, this crashed unpredictably for me. The crash almost certainly happens in write_callback or write_sample. My guess is that the crash is due to some issue in lines 73 - 94, where there is some pointer arithmetic.
A command line parameter which specifies the delay during the main loop is provided. Setting it to higher values sometimes allows the program to run without crashing.
Build instructions
nimble install soundio
nim c beep.nim
Usage
./beep 1000 # 1000 milliseconds of delay (generally runs without crashing for me)
./beep 10 # 10 milliseconds of delay (almost always crashes before the beep finishes)
./beep # 0 milliseconds of delay (almost always crashes before the beep finishes)
My system info
TL;DR This code crashes, somewhere in write_callback: https://gitlab.com/snippets/1883764
Thanks for the help!
Typically callbacks need a call to:
https://nim-lang.org/docs/system.html#setupForeignThreadGc.t
I've not gotten to check beyond a point but that's worth trying. Doubt it cause it works in debug mode but all the same.
Seems like everything works if I just compile with --threads:on. I didn't need to call setupForeignThreadGc, but I think I will leave it in the code anyway because it seems like its use case.
Thanks @shashlick