XMMatrix Exception with DirectXTK UWP

Dec 11, 2015 at 7:18 PM
Edited Dec 11, 2015 at 7:20 PM
Hi Chuck --

After converting my project to UWP version of DirectXTK I ran into a strange runtime exception ("BreakpointException") which occurs without exception details. I confirmed this behavior occurs with UWP and VS2015 using the v140 toolset but not with v120. Link: http://signcast.blob.core.windows.net/1.png

More about the exception. It occurs when a C++/CX "ref class" shared across a managed boundary is deallocated, and that class contains a private XMMATRIX member. If I use XMFloat4X4 the object is deallocated without error. I do not need to use the XMMATRIX member -- it's mere presence in the private section of the C++/CX class header is sufficient to cause an exception when the object destructor is called.

I understand that only primitive types and other ref class types may be shared with managed code. However, I was under the impression that private members which are not part of the public interface could be native C++ types.

Chuck, I consider you an expert and I thought I would share this information with you to see if this could be a bug worth reporting to the compiler team.

Another thought... that this is somehow related to byte alignment issues -- but that raises the question of why this would change in the UWP world with v140 and also how it would apply to private members. I see the same behavior with release vs debug and x86 vs x64.

My workaround is to use XMFloat4x4. Link: http://signcast.blob.core.windows.net/2.png

Thank you for your consideration on this unusual behavior. As always, I appreciate your expertise and insight.

Thanks,
Shaun
Coordinator
Dec 12, 2015 at 9:28 AM
WinRT classes can easily perturb the alignment, as can things like std::enable_shared_from_this.

Really the only way to guarantee alignment is to allocate the memory internal to your class with _aligned_malloc(x,16); This is what I do for DirectX Tool Kit internally: It uses the pImpl idom and the Impl class uses _aligned_malloc via the internal AlignedNew class. That way I can use XMVECTOR / XMMATRIX directly in the Impl class.

Otherwise, XMFLOAT4X4 (or using the SimpleMath wrapper Matrix class which does this as well) is the way to go.
Dec 14, 2015 at 7:50 PM
Thank you, Chuck. Your response helped me get on the right track. Thank you!