Monday, 4 November 2013

We’ll make an exception, just for you! Fun with accelerometers on Windows 8.1

I've been getting repeated failures on one of my apps that uses accelerometers, and I've been guessing where the failure was.  As the cert team wouldn't send me crash reports, I ended up building a version that I had no intention of releasing, but puts up in your face stack traces.

This time, I got a result, but the findings are extremely worrying.

I have code in my app that goes like this:

Accelerometer _accelerometer;

void NewReading(object sender, AccelerometerReadingChangedArgs e)
  ... nothing of interest here...

private void Initialize()
    _accelerometer = Accelerometer.GetDefault();
   if (_accelerometer != null)
        // the next line throws an exception!
        _accelerometer.ReadingChanged += NewReading;

This throws an exception as follows:

The object identifier does not represent a valid object: (Exception from HRESULT 0x800710D8)
Call Stack
  at Windows.Devices.Sensors.Accelerometer.add_ReadingChanged(TypedEventHandler`2 handler)
System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeMarshal.NativeOrStaticEventRegistr...(Func`2 addMethod, Action`1 removeMethod, T handler)
at System.Runtime.InteropServices.WindowsRuntime.WindowRuntimeMarshal.AddEventHandler[T](Func`2 addMethod, Action`1 removeMethod, T handler)

This looks like a hardware driver problem to me, and anyone who is tested on this particular computer will fail.

The event log said it was:

Any other developers out there seeing cert failures on Windows 8.1 like this?


  1. It appears that this was caused by not setting the ReportInterval property on the accelerometer before wiring up the ReadingChanged handler.

    Apparently you get a default value sometimes, but not always. My tablets were obviously reading the accelerometer (to process orientation I suspect).

  2. I have a Dell Venue 8 Pro on which my app throws that exception when the ReadingChanged handler is wired up. I tried setting the ReportInterval before setting the handler, but the exception is still thrown.
    If I read the ReportInterval before setting it, it returns 125, and MinReportInterval returns 16. So, default values are defined but the exception still happens.

    Any suggestions on solving this other than surrounding the offending code with a try/catch and disabling the accelerometer if an exception is thrown?

    The same exception is thrown for SimpleOrientationSensor object as well.