-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug: possible memory leak #27
Comments
Yes, continuously calling However, it is not recommended to do so. Instead, in order to keep an updated list of devices you should use Something like this: using CoreAudio;
internal class Program {
private static void Main(string[] args) {
var devices = new List<MMDevice>();
var enumerator = new MMDeviceEnumerator(Guid.Empty);
foreach(var device in enumerator.EnumerateAudioEndPoints(DataFlow.All,DeviceState.Active)) {
devices.Add(device);
}
var nc = new MMNotificationClient(enumerator);
nc.DeviceAdded += (sender, e) => {
lock(devices) {
MMDevice device = enumerator.GetDevice(e.DeviceId);
devices.Add(device);
}
};
nc.DeviceRemoved += (sender, e) => {
lock(devices) {
MMDevice device = enumerator.GetDevice(e.DeviceId);
devices.Remove(device);
}
};
nc.DeviceStateChanged += (sender, e) => {
// What to do here is really up to you...
lock(devices) {
MMDevice device = enumerator.GetDevice(e.DeviceId);
if((e.DeviceState & DeviceState.Active) == DeviceState.Active) {
devices.Add(device);
} else {
devices.Remove(device);
}
}
};
Task.Run(async () => {
while(true) {
lock(devices) {
foreach(var device in devices) {
var deviceName = device.DeviceFriendlyName;
// Not required because we never called GetAudioEndpointVolume or GetAudioSessionManager2
//device.Dispose();
// Do something with the device's name...
}
}
await Task.Delay(10);
}
}).Wait();
}
} Please avoid using |
Thank very much for a quick answer! |
Is session handling supposed to be done in similar fashion - OnSessionCreated? I've created a simple example based on the code above that iterates the sessions and I can still see memory usage creeping up slowly despite disposing "everything".
|
I haven't confirmed that yet but it looks like even the basic setup with events leak a bit of memory? |
I'll be honest, I'm confused at this point :D
|
At first glance, your code looks fine... I will run it locally to see if I can figure out what's going on. |
To be honest, I'm not sure what's going on... but I feel that this appears to be normal behavior. I followed the instructions found here and couldn't find anything useful. After all, the app sits there, doing nothing in an infinite loop... there's no chance that the CoreAudio library is leaking memory if it's not being used. Here's one test you can do... remove the code related to CoreAudio and run the app with just the infinite loop and you will see that, although not as fast, the process memory slowly, but steadily, keeps growing. Maybe someone with a deeper understanding of dotnet's inner workings (I'm thinking of @Elfocrash) could shed some light on this issue... |
Ohhh, that is more than interesting! |
Consider this code: internal class Program {
private static void Main(string[] args) {
Task.Run(async () => {
while(true) {
await Task.Delay(10);
}
}).Wait();
}
} I just asked CoPilot why this code is leaking memory... the answer is just what one would've expected:
However, it got me thinking: what if we are blocking some internal dotnet process? So, changing the code slightly, running the infinite loop on a separate thread seems to solve the problem: internal class Program {
private static void Main(string[] args) {
Thread t = new(() => {
while(true) {
Thread.Sleep(10);
}
});
t.Start();
}
} Another thing is that running this in VS still presents the apparent memory leak. But running it with |
Hello,
I've been experimenting with various "CoreAudio" wrapper libraries and it looks like most of them, including this one, appear to be leaking memory - slowly but constantly (or it's me using the library wrong, not excluding that :D)
I have a memory dump created at the end of the tests however github allows attachments only up to 25MB - compressed dump is 30MB+. I can deliver it via preferred method if required.
Test project is a clean new one with CoreAudio installed via nuget.
Code used for testing (put directly into main function, please ignore the var name typo):
Results:
The text was updated successfully, but these errors were encountered: