Why am I getting Access Violation Reading with XMVector3TransformCoord

Started by
3 comments, last by Adam_42 9 years, 2 months ago

I'm using DirectXMath.h and included the name space DirectX inside my lib.

When the camera updates it keeps going into this Access Violation Reading Location At -- on this line:


cameraRotationMatrix = XMMatrixRotationRollPitchYaw(cameraPitch, cameraYaw, 0);
		
cameraEye = XMVector3TransformCoord(defaultForward, cameraRotationMatrix); //-- ACCESS VIOLATION READING EXCEPTION!

cameraEye = XMVector3Normalize(cameraEye);

For the compile settings I set it to use SSE2 architecture and fp:precise. I read the MSDN and it said use DiretX:XMMATRIX and DirectX:XMVECTOR if you guarenteed it is 16 bit aligned or exceptions will occur at run time.

when the camera class is constructed I have this:


camera::camera() {

	defaultRight = XMVectorSet(1.0f, 0.0f, 0.0f, 0.0f);
	defaultForward = XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f);
	cameraRight = XMVectorSet(1.0f, 0.0f, 0.0f, 0.0f);
	cameraForward = XMVectorSet(0.0f, 0.0f, 1.0f, 0.0f);
	
	cameraYaw = 0.0f;
	cameraPitch = 0.0f;
	moveLeftRight = 0.0f;
	moveForwardBack = 0.0f;
}

Sometimes it runs and sometimes it will cause the exception. How should I go about fixing this?

Game Engine's WIP Videos - http://www.youtube.com/sicgames88
SIC Games @ GitHub - https://github.com/SICGames?tab=repositories
Simple D2D1 Font Wrapper for D3D11 - https://github.com/SICGames/D2DFontX
Advertisement

Well interestingly, I did more research and it was told the class address wasn't proply aligned and to use _mm_malloc function. Which I did and it works. I remembered to free the class when done with _mm_free function as well. That's something I learned new today.

Game Engine's WIP Videos - http://www.youtube.com/sicgames88
SIC Games @ GitHub - https://github.com/SICGames?tab=repositories
Simple D2D1 Font Wrapper for D3D11 - https://github.com/SICGames/D2DFontX

ACCESS VIOLATION READING EXCEPTION!

Reading which variable?

Also, where and how are cameraRotationMatrix and cameraEye declared?

EDIT: Glad you solved the problem.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Well interestingly, I did more research and it was told the class address wasn't proply aligned and to use _mm_malloc function. Which I did and it works. I remembered to free the class when done with _mm_free function as well. That's something I learned new today.


In 32 bit mode objects allocated on the heap are aligned to an 8 byte boundary. Meanwhile the non-unaligned load/store operations require paragraph alignment (16 bytes) for cache reasons. As such, sometimes things "might" work when you don't allocate them using an aligned allocator, and some might end up unaligned (as you found out).

In C there's an aligned_alloc function, and in C++ you can use the std::align function to return an aligned pointer from a block of memory (of course, this does require you to allocate sufficient memory to fit the aligned object plus however is necessary to align it). Do note that if you simply point a class at your aligned memory... a constructor will not be called. So if you have non-trivial constructors you need to use placement new, and if you have a non-trivial destructor you will need to invoke that as well.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

The simple option is to override global operator new and call _aligned_malloc (or similar) from it. If you do that you can make every allocation 16-byte aligned, with only a few lines of code in one place.

This also means you can do things like putting aligned types in a std::vector.

Note that there's several variants of operator new and delete that you'll need to replace - you want to change the non-throwing variants as well.

This topic is closed to new replies.

Advertisement