Hey guys
I'm using the FMOD C library for audio playback which creates its own playback thread which periodically calls my Nim callback proc to fill up the audio buffer.
It turns out that I need to call setupForeignThreadGc() at the top of my callback function, otherwise I'm getting random intermittent crashes. The backend integration documentation is kind of vague about the correct usage of this function, is this how I am supposed to use it? Just put setupForeignThreadGc() at the top of my Nim callback proc and that's it?
This seems to do the trick anyway, no crashes since and I'm not leaking memory either (I don't really care about cleanup, the playback thread is always alive until I terminate the program). I'd just like to understand a bit better what I'm doing, so any clarification is welcome (especially because this will be part of a blog post I'm going to write about Nim and I don't want to spread misinformation!).
Thanks guys!
In a nutshell, if C code creates a thread and calls any Nim code, that Nim code needs to have the GC present behind the scenes to run correctly. Without this, it will crash.
You can set it up using setupForeignThreadGc() and at the end of the thread, call tearDownForeignThreadGc() to clean up.