Wireless Communication Library Support Forum

Frameworks => Bluetooth Framework => Topic started by: dartecs on January 29, 2015, 06:13:23 PM

Title: Discovered wclBluetoothDevices never removed?
Post by: dartecs on January 29, 2015, 06:13:23 PM
I am currently trying to fix some memory leak with WCL. I saw the "Memory Leak" topic regarding MS stack in this forum but my problem seems to be another one. It looks as if a once discovered wclBluetoothDevice remains in memory forever. (At least that is what my memory testing tool says).

I tested with your BluetoothDiscoveryDemo application and 5 BT devices.


Is this a "bug" in WCL or do I misunderstand something here?
Title: Re: Discovered wclBluetoothDevices never removed?
Post by: Mike Petrichenko on January 29, 2015, 06:41:51 PM
Hello,

Its not actually bug in WCL as .NET uses Garabge Collection and when it desides to disposed unused object only MS knows. However we may think on calling garbage colletor in some WCL places.
Title: Re: Discovered wclBluetoothDevices never removed?
Post by: dartecs on January 30, 2015, 10:37:31 AM
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.
Title: Re: Discovered wclBluetoothDevices never removed?
Post by: Mike Petrichenko on January 30, 2015, 11:21:54 AM
Ok, thank you for the report. We will check that out and fix possible bug.
Title: Re: Discovered wclBluetoothDevices never removed?
Post by: Mike Petrichenko on February 06, 2015, 12:21:41 PM
Hello,

Please, try attached test version.

[attachment deleted by admin]