Author Topic: GetPaired NullReferenceException with new WCL Version 6.14  (Read 18083 times)

dartecs

  • Guest
GetPaired NullReferenceException with new WCL Version 6.14
« on: February 20, 2015, 04:59:51 PM »
After updating from WCL 6.13.10.0 to WCL 6.14.0.0 I get an exception on this call to GetPaired:

        public void UpdatePairedStatus()
        {
            bool oldStatus = mIsPaired;
            if (Radio != null && mDevice != null)
            {
                Device.GetPaired(Radio, ref mIsPaired);
            }
            else
            {
                mIsPaired = false;
            }
            if (oldStatus != mIsPaired && PairingStatusChanged != null)
            {
                PairingStatusChanged(this, null);
            }
        }


With 6.13.10.0 this same code works just fine. Any idea why? (Details of the exception are in the attachment.)

[attachment deleted by admin]

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3675
  • Karma: 1000
    • Wireless Communication Libraries
Re: GetPaired NullReferenceException with new WCL Version 6.14
« Reply #1 on: February 20, 2015, 05:42:00 PM »
Hello,

I think Device object is null or something or Radio object has not been initialized properly. We fixed a lot of memory leaks in latest WCL 6.14.0.0 and lot of object which has not been disposed in early version now correctly disposed. Probably your code uses one of that fixed bug.

Also, you check mDevice but use Device - those are different variables and I think Device is null.

dartecs

  • Guest
Re: GetPaired NullReferenceException with new WCL Version 6.14
« Reply #2 on: February 24, 2015, 11:00:55 AM »
Device references mDevice. So in matter of functionality it doesn't matter which one I use. (But of course it matters in regards of clean code and I will change it...)
Also from the details of the exception (attached to my first post) one can see that the exception is thrown inside WCL: Module = wcl.dll. If Device would really be null as you assumed then the exceptions module would be my own Module = Bluetooth.dll. So I still suspect there to be a bug in the new WCL version.

My suspision is that wclBluetoothRadio objects are now disposed to early in some cases. To verify this I made a small test application which is attached to this post. What I found:
  • The OnDiscoveryComplete event gives a reference to the discovered device and the radio it was discovered on.
  • When the reference to that radio is later used again I get an object disposed error

To see what I mean please start the test application, click "Discover Devices", wait until some devices are displayed, then click "Check Paring Status". This will lead to an exception.

So to me that seems as if WCL would dispose a radio despite it still being referenced by my test code. This is not the behaviour I would expect. Could you please confirm if this is a bug or am I misunderstanding something here?

[attachment deleted by admin]

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3675
  • Karma: 1000
    • Wireless Communication Libraries
Re: GetPaired NullReferenceException with new WCL Version 6.14
« Reply #3 on: February 24, 2015, 11:24:54 AM »
Hello,

The objects passed into OnDiscoveryComplete (Radio and Devices) are valid only inside event handler (that is correct for any objects passed into any event handler). If you want to use them outside the event handler then you must create a copy (create new object instance and then call Assign method).

dartecs

  • Guest
Re: GetPaired NullReferenceException with new WCL Version 6.14
« Reply #4 on: February 24, 2015, 12:59:54 PM »
OK, at the risk of making a complete fool of myself I dare to ask: Why should objects passed into an event handler only be valid inside that handler? Isn't it in C# objects should be valid as long as there are references to that object?

Offline Mike Petrichenko

  • Bluetooth Framework Developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 3675
  • Karma: 1000
    • Wireless Communication Libraries
Re: GetPaired NullReferenceException with new WCL Version 6.14
« Reply #5 on: February 24, 2015, 01:04:15 PM »
Well. Actualy WCL has native code and communicates with hardware and must release resources as soon as possible. As WCL uses the same code for all editions (jsut different laguages: VCL uses Pascal, C++ Edition uses C++, .NET uses C++/CLR) it has identical implementation. In unmanaged (native) languages the common rule is that the code which creates an object must release that object. So all passed into events has been created by WCL code and disposed by WCL code as soon as possible (imediately when event done). Even it looks as .NET object internally WCL uses lot of native reasource which can not wait when GC disposes them.

So by other words: this is by design.

 

Sitemap 1 2 3 4 5 6 7