NAudio - DirectSoundOut in memory even after disposal [Answered]

Zzonedetec10/19/2022
Hello my friends,

This is my code :

            Stream str = String.IsNullOrEmpty(fromUri) ? getStreamFromKey(e, state) : System.Reflection.Assembly.GetEntryAssembly().GetManifestResourceStream(fromUri)!;

            WaveFileReader wfr = new WaveFileReader(str);
            WaveChannel32 wc = new WaveChannel32(wfr);
            DirectSoundOut wfo = new DirectSoundOut();
            wfo.Init(wc);
            wfo.Play();

            wfo.PlaybackStopped += (sender, e) =>
            {
                wfo.Dispose();
                wc.Dispose();
                wfr.Dispose();
            };


This is the code that make my program laggy after 40-50 seconds (note that this code is repeated 3-4 times each second)
+ After this time the sound become laggy, like saturated for no reason.

Can someone help me fix this code ? I don't know what to do more than "Dispose()"
PS: you can see in the image the object on NAudio that had been created

Thanks 🙂
Image
Zzonedetec10/20/2022
please 🥺
XWYBxianzai wo you bingqiling10/20/2022
you didnt dispose the stream?
KKouhai10/20/2022
Are you re-reading the file 3-4 times per second?
Zzonedetec10/20/2022
I've just done it, it still laggy after a minute
Zzonedetec10/20/2022
yes because I am doing a keyboard game and everything you press a key it make a short sound
KKouhai10/20/2022
First off, that would hinder performance.
Second, add a method that you can execute when it get's laggy, in that method call Gc.Collect() and see if it makes a difference
Zzonedetec10/20/2022
Still laggy :/
Zzonedetec10/20/2022
Maybe there's just a better library to plat audio?
KKouhai10/20/2022
I don't think there's a memory leak in NAudio, can you send a screenshot of the objects after calling Gc.Collect?
Zzonedetec10/20/2022
Image
Zzonedetec10/20/2022
I just putted it here
Zzonedetec10/20/2022
so when it's become laggy I press on escape but nothing change
KKouhai10/20/2022
Oh, I meant a SS of the profiler 😅
Zzonedetec10/20/2022
a what of the what? 🤠
Zzonedetec10/20/2022
Oh
Zzonedetec10/20/2022
I see what you meant sorry
Zzonedetec10/20/2022
it's just all new
Zzonedetec10/20/2022
wait i'm doing it
KKouhai10/20/2022
Np
Zzonedetec10/20/2022
this is before and...
Image
Zzonedetec10/20/2022
Image
Zzonedetec10/20/2022
looks like it removed some obejct yes
Zzonedetec10/20/2022
Image
Zzonedetec10/20/2022
so the problem isn't here o_o
KKouhai10/20/2022
Okay, where are you disposing the DirectSoundOut?
Zzonedetec10/20/2022
Image
Zzonedetec10/20/2022
here, just after the audio finish
KKouhai10/20/2022
PlaybackStopped won't be called because you're using WaveChannel32
Zzonedetec10/20/2022
oh yeah?
KKouhai10/20/2022
Try using Wave16ToFloatProvider
Zzonedetec10/20/2022
there is no .Dispose() for this one, is this normal?
KKouhai10/20/2022
Yes, that's okay
Zzonedetec10/20/2022
it works!!!
Zzonedetec10/20/2022
no more laggy audio
Zzonedetec10/20/2022
!!
Zzonedetec10/20/2022
thanks a lot 😁 😁 😁
KKouhai10/20/2022
The problem with WaveChannel32 is it never stops providing data even after the file's been fully read 😄
Zzonedetec10/20/2022
I did just copy past the code from the internet because I sucks with NAudio ^^' xD
Zzonedetec10/20/2022
thanks again I'm happy now c:
KKouhai10/20/2022
Np, make sure to remove Gc.Collect as well 👍
Zzonedetec10/20/2022
yeah thank you c:
AAccord10/20/2022
✅ This post has been marked as answered!