Author Topic: Discovered wclBluetoothDevices never removed?  (Read 16603 times)

dartecs

  • Guest
Discovered wclBluetoothDevices never removed?
« 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.

  • Whenever I click [Discover] the list of devices is refreshed and correctly shows my 5 BT devices.
  • I can click [Discover] over and over again and the list always refreshes to my 5 BT devices. Everything as expected.
  • But the wcl.wclBluetoothDevice object count in my memory is increased by 5 after every new discovery. This is not what I would expect. I would expect the number of wcl.wclBluetoothDevice objects to remain at 5.

Is this a "bug" in WCL or do I misunderstand something here?

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3675
  • Karma: 1000
    • Wireless Communication Libraries
Re: Discovered wclBluetoothDevices never removed?
« Reply #1 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.

dartecs

  • Guest
Re: Discovered wclBluetoothDevices never removed?
« Reply #2 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.

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3675
  • Karma: 1000
    • Wireless Communication Libraries
Re: Discovered wclBluetoothDevices never removed?
« Reply #3 on: January 30, 2015, 11:21:54 AM »
Ok, thank you for the report. We will check that out and fix possible bug.

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3675
  • Karma: 1000
    • Wireless Communication Libraries
Re: Discovered wclBluetoothDevices never removed?
« Reply #4 on: February 06, 2015, 12:21:41 PM »
Hello,

Please, try attached test version.

[attachment deleted by admin]

 

Sitemap 1 2 3 4 5 6 7