Memory access exception when using XMVECTOR

0

I'm making pong in DirectX11 and I'm getting some weird error.

I have a pointer declared inside my Pong class:

XMVECTOR *ballDirection;

And for some reason, whenever I try to access it:

Unhandled exception at 0x002127d8 in DirectX11Pong.exe:
  0xC0000005: Access violation reading location 0x00000000.

I'm pretty sure this is what happens when you try to access a nullptr pointer, but, before any "accessing" to the pointer happens, I have it initialized:

ballDirection = new XMVECTOR();

For example, the line of code right now I'm getting this error at is the following:

*ballDirection = XMVectorSetX(*ballDirection, 1);

The only other information that I think is relevant is that I tried earlier to turn a single pointer that holds the paddle info into an array of pointers (for multiple players):

(Before)

Sprite *paddle;

(After)

Sprite *paddle[2];

The moment I did this, I got this error accessing ballDirection, even though it worked perfectly before I made this array, and I changed no code to do with ballDirection while creating the array and modifying the appropriate code.

After I noticed getting this error I changed the code back, and this still happened.

How can I fix this?

c++
windows
memory-management
asked on Stack Overflow Oct 28, 2012 by Aaron • edited Oct 25, 2018 by Öö Tiib

2 Answers

4

Microsoft does say about dynamic allocation of XMVECTOR that:

Allocations from the heap, however, are more complicated. As such, you need to be careful whenever you use either XMVECTOR or XMMATRIX as a member of a class or structure to be allocated from the heap. On Windows x64, all heap allocations are 16-byte aligned, but for Windows x86, they are only 8-byte aligned.

So you should not just new XMVECTOR there and expect that all works.

answered on Stack Overflow Oct 28, 2012 by Öö Tiib • edited Oct 28, 2012 by Öö Tiib
1
  1. Always check your allocations! At least macro something noobish like that:

    #if defined(DEBUG) || defined(_DEBUG)
    #ifndef XBOOL
        #define XBOOL(x)                                                                \
        {                                                                               \
            if(!(x))                                                                    \
            {                                                                           \
                MessageBox(0, L"Error running: "L#x, L"Error", MB_OK | MB_ICONSTOP);    \
                return false;                                                           \
            }                                                                           \
        }
    #endif
    #else
        #ifndef XBOOL
            #define XBOOL(x) (x);
        #endif
    #endif 
    

Usage:

XBOOL(ballDirection = new XMVECTOR())

2. Use debugger! Set up some breakpoints, check pointer value from a place of allocation to place of error in every line.

answered on Stack Overflow Oct 28, 2012 by Ivan Aksamentov - Drop • edited Oct 28, 2012 by Ivan Aksamentov - Drop

User contributions licensed under CC BY-SA 3.0