Kyle-Gagner / PortAudio.Net

Cross platform .NET wrappers for PortAudio
Mozilla Public License 2.0
12 stars 7 forks source link

Callback delegate is garbage collected #5

Closed simonhaines closed 2 years ago

simonhaines commented 3 years ago

Hi Kyle, thanks for making this library. I particularly like the PaBuffer implementation for managing native buffers.

One small issue is that passing the Callback method of the StreamCallbackContainer in the call to OpenStream creates a new delegate which is then subject to garbage collection. My fix for this is to pre-allocate the delegate in the StreamCallbackContainer constructor ensuring it has the same lifetime. Other solutions may include using Marshal.GetFunctionPointerForDelegate, but this is simple and it works.

I have committed my fix to the callback-bugfix branch of my fork here and am happy to submit a pull request if you like.

Kyle-Gagner commented 2 years ago

Hi @simonhaines , I just ran into this issue myself. I have pushed a fix. Please be aware that your fork is missing the same fix for the stream finished callback. Wish I had the time / energy to do PortAudio.Net justice, maybe someday :)

simonhaines commented 2 years ago

Thanks @Kyle-Gagner I know what you mean regarding time/energy. I'm not sure how I missed that stream finished callback as well, but I'll switch to your branch and remove the callback-bugfix branch in my fork.