简体   繁体   English

C ++中的DirectX纹理清理

[英]DirectX Texture Clean-up in C++

I am currently working on a game in C++ using DirectX. 我目前正在使用DirectX开发C ++游戏。 The engine was given to us by a professor at my school, and I have been inspecting memory leaks in his engine and I believe I have traced them back to this method. 该引擎是由我学校的一位教授给我们提供的,我一直在检查其引擎中的内存泄漏,我相信我可以将其追溯到这种方法。 To start, it wasn't even being called, and now that it's being called I am unsure if this is the correct way to completely delete and clean up textures loaded by DirectX. 首先,它甚至没有被调用,现在它被调用了,我不确定这是否是完全删除和清除DirectX加载的纹理的正确方法。

Here is the method being called on the TextureManager : 这是在TextureManager上调用的方法:

void DirectXTextureManager::clear()
{
    map<wchar_t*, LPDIRECT3DTEXTURE9>::iterator it;
    map<wchar_t*, LPDIRECT3DTEXTURE9>::iterator itToErase;
    it = textures->begin();
    while (it != textures->end())
    {
    wchar_t *keyToDelete = (*it).first;
    LPDIRECT3DTEXTURE9 textureToDelete = (*it).second;
    itToErase = it;
    it++;
    textures->erase(itToErase);
    delete keyToDelete;
    textureToDelete->Release();
    textureToDelete = NULL;
    }

    stringTable->emptyStringTable();
}

Regarding the textures part everything seems fine - it is enough to call Release() on the LPDIRECT3DTEXTURE object to free it: 关于纹理部分,一切似乎都很好-只需调用LPDIRECT3DTEXTURE对象上的Release()即可释放它:

LPDIRECT3DTEXTURE9 textureToDelete = (*it).second;
...
textureToDelete->Release();

I'm a bit worried about stringTable - is it the part responsible for wchar_t* in the map keys? 我有点担心stringTable它是映射键中负责wchar_t*的部分吗? If so, you should check what stringTable->emptyStringTable() does, maybe there's no need to delete the keys manually. 如果是这样,则应检查stringTable->emptyStringTable()作用,也许不需要手动删除键。

Does DirectXTextureManager have any other methods that call Release() on any textures? DirectXTextureManager是否具有在纹理上调用Release()其他方法?

If it does, then perhaps there is no need to call clear() . 如果是这样,那么也许就不需要调用clear() In particular, check whether the class destructor or some other " UniInitialize() " method exists that may already do the cleanup. 特别是,检查是否存在可能已经进行了清理的类析构函数或其他“ UniInitialize() ”方法。 Personally, I also prefer to use the SAFE_RELEASE() macro from the DXUT headers since is checks if the pointer is non-null before calling Release and sets it to NULL afterwards. 就个人而言,我也更喜欢使用DXUT标头中的SAFE_RELEASE()宏,因为它在调用Release之前检查指针是否为非null,然后将其设置为NULL。

Finally, you might find debugging easier with the output from the DirectX debug run-time that is controlled from the "DirectX Control Panel" (installed with the SDK). 最后,使用“ DirectX控制面板”(与SDK一起安装)控制的DirectX调试运行时的输出,您可能会发现调试更容易。 It will output very good logging to the VisualStudio output window including details of unreleased resources on shutdown that you can use to troubleshoot the code. 它会将非常好的日志记录输出到VisualStudio输出窗口,其中包括关闭时未释放资源的详细信息,您可以使用这些资源来对代码进行故障排除。

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

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