Joey Arnold Andres
Joey Arnold Andres

Reputation: 368

DirectX 11 Memory Deallocation Error

I recently finnish a simply 2d game engine. In the sprite module of my project, there's an exeption about

"Unhandled exception at 0x00CE4A75 in AI.exe: 0xC0000005: Access violation reading location 0xCCCCCCCC."

I don't know what causes it because everything is initialize and deallocated the same. And this exception seems to happen in the if( m_inputLayout ) m_inputLayout->Release(). Everything else is fine. The code is right down below.

CAIGESprite::~CAIGESprite(void)
{
    if( m_mvpCB ) m_mvpCB->Release();
    if( m_alphaBlendState ) m_alphaBlendState->Release();
    if( m_colorMapSampler ) m_colorMapSampler->Release();
    if( m_colorMap ) m_colorMap->Release();
    if( m_vertexBuffer ) m_vertexBuffer->Release();
    if( m_inputLayout ) m_inputLayout->Release();
    if( m_solidColorPS ) m_solidColorPS->Release();
    if( m_solidColorVS ) m_solidColorVS->Release();
    if( m_textureFile ) delete m_textureFile;
    if( m_shaderFile ) delete m_shaderFile;

    m_shaderFile = nullptr;
    m_textureFile = nullptr;
    m_solidColorVS = NULL;
    m_solidColorPS = NULL;
    m_inputLayout = NULL;
    m_vertexBuffer = NULL;
    m_colorMap = NULL;
    m_colorMapSampler = NULL;
    m_alphaBlendState = NULL;
    m_mvpCB = NULL;
}

I also checked the content of each of them and they are all empty Unable to read memory, so why would the m_inputLayout be different and cause an exeption? What are the possible reasons.

I'd post more code if requested.

Upvotes: 0

Views: 250

Answers (2)

davidotcom
davidotcom

Reputation: 51

Was "m_inputLayout" properly initialized to null in the constructor? You might be trying to delete an invalid pointer that hasn't been created.

Upvotes: 0

user2520968
user2520968

Reputation: 368

Remember that the "if" operator only tests boolean conditions. While it is common to use "if" for null checks directly without specifying " != 0 ", it does implicit cast of your pointer to boolean. If your pointer is 0, the value is false, and if it is anything but zero, the value is true. In the even that the variable has not been initialized, the memory location can hold any garbage value, but in the Debug mode, you usually get a 0xCCCCCCCC or some other known constant to help you figure out what the problem is. So, in my opinion, your variable has not been initialized. If you step through the code you should see that its value is "0xCCCCCCCC". By the naming of your variable, I presume that it is a member variable, so the good practice is to set it to NULL in the initializer list of the class to which it belongs.

Upvotes: 1

Related Questions