Native Code on Windows Phone 8: The assert Problem

So there you are, cranking out some new C/C++ (or CX/C++) code on Windows Phone. Its all new, so you add asserts all over the place to make sure its working. You run your app under the debugger to see how far it gets, and boom: it exits for no obvious reason. Turns out assert is broken on WP8.

First off ensure you have the Native debugger set on the project's property page (it defaults to Managed). Note that unlike the Desktop you can't debug both managed and native code at once. (The phone debugger is like Highlander: there can only be one).

After a few repeated attempts where I thought I might be dreaming, I tried to figure out why my process was exiting. The tried-and-true breakpoint of

{,,kernelbase.dll}ExitProcess

didn't even bind (once I had configured VS to use the Microsoft Symbol Server). Turns out that to intercept process exit in the debugger,

{,,ntdll.dll}RtlExitUserProcess

is the breakpoint to use. This hit, and it turns out someone forgot to update the C-runtime code for assert to make it work on WP8, so it tries (and fails) to put up a MessageBox, then aborts the process.

Thanks to James McNellis on the VS Tools team, the way to get assert to work a little better is to add this to the startup of your app:

#ifndef NDEBUG   
signal(SIGABRT, [](int)
{
   __debugbreak();  
}); 
#endif

This makes the assert stop in the debugger, at least. If you want to know what the assert message is and where in the code it is you'll have to use the callstack, but this is a huge improvement over the in-box behavior. Thanks James!