简体   繁体   English

DirectX 11内存释放错误

[英]DirectX 11 Memory Deallocation Error

I recently finnish a simply 2d game engine. 我最近完成了一个简单的2D游戏引擎。 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." “ AI.exe中0x00CE4A75处未处理的异常:0xC0000005:访问冲突读取位置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() . 而且此异常似乎发生在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? 我还检查了它们的内容,它们都是空的。 无法读取内存 ,那么为什么m_inputLayout会不同并导致异常? What are the possible reasons. 可能是什么原因。

I'd post more code if requested. 如果需要,我会发布更多代码。

Remember that the "if" operator only tests boolean conditions. 请记住,“ if”运算符仅测试布尔条件。 While it is common to use "if" for null checks directly without specifying " != 0 ", it does implicit cast of your pointer to boolean. 虽然通常直接使用“ if”进行null检查而不指定“!= 0”,但是它会将您的指针隐式转换为布尔值。 If your pointer is 0, the value is false, and if it is anything but zero, the value is true. 如果您的指针为0,则该值为false;如果它不是零,则为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. 即使该变量尚未初始化,内存位置也可以保存任何垃圾值,但是在“调试”模式下,通常会得到一个0xCCCCCCCC或其他已知常量,以帮助您找出问题所在。 So, in my opinion, your variable has not been initialized. 因此,我认为您的变量尚未初始化。 If you step through the code you should see that its value is "0xCCCCCCCC". 如果单步执行代码,则应看到其值为“ 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. 通过变量的命名,我假定它是成员变量,因此,好的做法是在其所属的类的初始化列表中将其设置为NULL。

Was "m_inputLayout" properly initialized to null in the constructor? 在构造函数中,“ m_inputLayout”是否正确初始化为null? You might be trying to delete an invalid pointer that hasn't been created. 您可能正在尝试删除尚未创建的无效指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM