intermittent debug only exception - SSEVector
Closed this issue · 2 comments
I get a crash in the debug build about 50% of the time. I am on a windows laptop, using VS2017. Here is the callstack:
CGFX5.exe!SSEVector::operator*(const SSEVector & other) Line 455 C++
CGFX5.exe!SSEVector::matrixMul(void * result, const void * mat1, const void * mat2) Line 26 C++
CGFX5.exe!Matrix::operator*(const Matrix & other) Line 160 C++
CGFX5.exe!runApp(SDLApplication * app) Line 152 C++
CGFX5.exe!main(int argc, char * * argv) Line 184 C++
Exception thrown at 0x000D4841 in CGFX5.exe: 0xC0000005: Access violation reading location 0xFFFFFFFF.
This only happens in the debug build - the release build always works fine.
Please let me know if you have any ideas.
Thanks in advance
PS - I can avoid the problem by disabling SIMD in debug builds...
This occurs because your operating system is allocating memory with 8-byte alignment, whereas SSE requires vectors to have 16-byte memory alignment. If memory is 8-byte aligned, then 50 percent of the time, it will by chance be 16-byte aligned and thus be correct, hence why this occurs only 50 percent of the time.
There is a solution to this: The project uses a custom overloaded "new" operator that enforces 16-byte aligned memory allocations. For some reason, this isn't being used in your debug build, but is during release (or the OS is enforing 16 byte alignment on it's own for release). Here's a suggestion: In https://github.com/BennyQBD/3DGameProgrammingTutorial/blob/master/src/core/memory.cpp edit the newFunc at the top so it prints something whenever memory is allocated. This should tell you if it's being called or not. Also double check that memory.hpp's default alignment for malloc is actually set to 16
Thanks Benny. Turns out I did not have this commit, where memory.cpp was added:
Commit: 6a638bf [6a638bf]
Author: BennyQBD bennyqbdspam@gmail.com
Date: Sunday, May 20, 2018 8:31:39 PM
Merge fixes from CGFX5
Once I got that commit, everything worked fine since it sets the default alignment to 16 and adds the newFunc code in memory.cpp.
I usually don't sync to your latest code, because I like to write the code myself as I watch your videos and follow along. In the future I'll try to cherry pick the important changes and bug fixes at least.
Keep up the great work and if there's anything I can help with, please let me know. Thanks!