NullReferenceException with UWP Starter Template

This discussion was created from comments split from: Metawear c# gyro stream stops after a while on windows UWP.


  • edited May 2016
    this issus still not fixed....

    System.NullReferenceException throw exception in mbl_mw_connection_notify_char_changed(cppBoard, response, (byte)response.Length); (Object reference not set to an instance of an object).

  • Hello hsutuo.  I think the issue you are talking about is different issue.  
    My issue was based on a clean connection to the metawear board.  Once connected, it only streamed for a  short time.  The commit that Eric made has caused great improvements.  

    I have occasionally got the issue you are referring to.  I have noticed that I get the error during the next run if I do not cleanly disconnect or tear down the board.  That is, the next time I would run, I would get that error after the scenario described.  In general, I have brought that error to a minimum by not stopping the program in visual studio  before I cleanly tear down the board.  
  • @hsutuo You need to provide more information than a error message.  
    • Does this error occur frequently?
    • Can you reliably reproduce it?
      • If so, what are the steps to cause the error?
    • What device and OS are you testing on?
    This seems rather peculiar.  If you see this error again, can you tell me which variable is null reference exception is for?  I have yet to encounter this error when closing the app via stopping the debugger when using the UWP template solution.
  • edited May 2016
    here's my project

    MetaWear CPro HW Rev:0.2 FW Rev: 1.2.1 Model Number:2
    os:Windows 10 x64(10586.218) Visual Studio Community 2015(update2)

    in release(x86/64) mode, app will crashed direct,  in debug(x86/64)  mode, it goes for several minutes and crashed (throw NullReferenceException or System.AccessViolationException stop at  mbl_mw_connection_notify_char_changed function in MetaWearBoard.cs)

  • You need to wrap callback functions with the appropriate delegate type from the Core namespace, in this case: FnVoidPtr.
  • For what it's worth, I have been fighting a similar problem all day today (exhibited the same exception when calling the same function) after about 30-60 seconds working fine.  After building a debug version of the C++ library, I was finally able to track it down.  I was wrapping my callback with the correct delegates, but I wasn't storing off a reference, like:

    mbl_mw_datasignal_subscribe(acc_signal, new FnVoidPtr(my_handler));

    I'm guessing .NET can't/doesn't keep a reference count due to the interop and ends up cleaning up the object since there are no references to it left in .NET.  I suppose I know, now, why the sample code has that strange dictionary of delegates :)
  • Mostly I am getting Null Reference Exception at following code and some time it works:
    notifyChar = btleDevice.GetGattService(GattCharGuid.METAWEAR_NOTIFY_CHAR.serviceGuid).GetCharacteristics(GattCharGuid.METAWEAR_NOTIFY_CHAR.guid).FirstOrDefault();

    Anybody know how to fix it? 
  • The callback function is wrapped in FnVoidPtr but still app crash after few seconds dumping accelerometer data.

    Any idea how to solve this issue?
  • Issue I fixed by keeping alive the references which are passed to the C# wrapper functions.
This discussion has been closed.