Answered by:
Windows::Devices::Sensors::Accelerometer::GetDefault() throws exception in Native Only mode

Question
-
How to recreate:
1. Create a Blank XAML App;2. In App::App() we initialize m_Accelerometer = Windows::Devices::Sensors::Accelerometer::GetDefault(); (It goes without saying that we have to declare m_Accelerometer in the header, and also add #include <windows.devices.sensors.h> there);
3. Enable all exceptions in the Debug mode;
4. Run the compiled app in Native Only mode.
See that the exception occurs where m_Accelerometer is being initialized.First-chance exception at 0x778A4B32 (KernelBase.dll) in App2.exe: 0x40080201: WinRT originate error (parameters: 0x80070490, 0x00000064, 0x0320EB84).
If there is a handler for this exception, the program may be safely continued.
Note that no such exceptions happen when you debug the project in Managed Only mode.
Desired outcome: m_Accelerometer must be initialized either to the default accelerometer or nullptr if no accelerometers are found. Also, Accelerometer::GetDefault() must not throw in Native Only mode (the same app behaves just fine in Managed Only mode).
As for the short-term, what kind of workaround you would suggest in this particular case?
- Moved by Jesse Jiang Thursday, November 1, 2012 2:31 AM (From:Building Windows Store apps with C++ )
Wednesday, October 31, 2012 9:55 AM
Answers
-
This is an Originate Error diagnostic message to provide more information, which means it should only surface as an exception when debugging is enabled AND first chance exceptions are enabled.
In non-debugging circumstances or when first chance exceptions are disabled (the default setup), this message/exception is silent.
In all circumstances, GetDefault() returns null when no sensors are found.
- Marked as answer by Justinas V. Daugmaudis Thursday, November 8, 2012 4:00 PM
- Edited by Lisa O - MSFT Saturday, November 10, 2012 9:15 PM
Tuesday, November 6, 2012 6:13 AM
All replies
-
Justinas,
0x80070490 is error not found. If an accelerometer is not connected to your system, then that exception is normal. Native-Only debugging mode just surfaces the exception as is.
Since you enabled all exceptions, you will see all exceptions, including first chance exceptions that are suppressed in the debugger. This won't affect your program execution in Release mode.
Regards,
lisa
Friday, November 2, 2012 5:04 AM -
But, doesn't this indicate an error, I mean why not simply return nullptr when calling Windows::Devices::Sensors::Accelerometer::GetDefault();Monday, November 5, 2012 9:18 AM
-
This is an Originate Error diagnostic message to provide more information, which means it should only surface as an exception when debugging is enabled AND first chance exceptions are enabled.
In non-debugging circumstances or when first chance exceptions are disabled (the default setup), this message/exception is silent.
In all circumstances, GetDefault() returns null when no sensors are found.
- Marked as answer by Justinas V. Daugmaudis Thursday, November 8, 2012 4:00 PM
- Edited by Lisa O - MSFT Saturday, November 10, 2012 9:15 PM
Tuesday, November 6, 2012 6:13 AM