OK, I see thist point. So I added a GC.Collect() to the discovery complete handler. This should force all unreferenced objects to be collected:
private void wclBluetoothDiscovery_OnDiscoveryComplete(object sender, wcl.wclBluetoothDiscoveryCompleteEventArgs e)
{
if (e.Devices == null)
MessageBox.Show("Discovery complete with error");
else
if (e.Devices.Count == 0)
MessageBox.Show("Nothing found");
else
for (uint i = 0; i < e.Devices.Count; i++)
{
wcl.wclBluetoothDevice Device = e.Devices;
ListViewItem Item = lvDevices.Items.Add(Device.Address);
for (int j = 0; j < 4; j++)
Item.SubItems.Add("");
Item.Selected = true;
lvDevices.Select();
RefreshDevice();
Item.Selected = false;
lvDevices.Select();
}
GC.Collect();
}
But still the same behaviour: 5 more wclBluetoothDevice objects after each [Discover].
Then I added a timer to the test app that fires every 100ms and calls the same code as the [Discover] button. (I also removed the "Started" message box from the OnDiscoveryStarted handler.)
private void timer1_Tick(object sender, EventArgs e)
{
if (!wclBluetoothDiscovery.Active)
{
wcl.wclBluetoothRadio Radio = GetSelectedRadio();
if (Radio != null)
{
wcl.wclErrors.wclShowError(wclBluetoothDiscovery.Discovery(Radio,
Convert.ToByte(edDiscoverTimeout.Text), true));
}
}
}
This leads to 211'650 wclBluetoothDevice objects using 17.76 MB after 1h 10minutes. I would expect the garbage collector to be running sometime before the application uses up that much memory.
So I still have the suspicion that wcl keeps some reference to the created wclBluetoothDevice and thus they can not get collected by GC. But I have to admit that I do not know for sure at what point the GC really does cleanup the heap.