简体   繁体   English

具有D3DX9的C ++-内存大小以10 MB /秒的速度不断增加-为什么?

[英]C++ with D3DX9 - Memory size keeps increasing at a rate of 10 MB/sec - Why?

I'm working on a 3D engine as school project. 我正在研究3D引擎作为学校项目。 I've installed virtual leak detector to get rid of memory leaks (and it worked, since I'm not getting any of those dumps anymore). 我已经安装了虚拟泄漏检测器来消除内存泄漏(并且可以正常工作,因为我不再需要这些转储了)。 Recently, I accidentally left my app running for 5 minutes... And my PC got slow as hell. 最近,我不小心使我的应用程序运行了5分钟。。。 I've tracked down the issue by commenting out lines and such, but I can't find out why C++ won't release my cleared memory until I close the application. 我已经通过注释掉行等找到了问题,但是我不知道为什么C ++在关闭应用程序之前不会释放清除的内存。

PROBLEM: "Internal" memory leak, looks like C++ is only deleting something once the app closes 问题:“内部”内存泄漏,似乎C ++仅在应用程序关闭后才删除某些内容

CODE: My render code (at pastebin Yz79Ck0b) 代码: 我的渲染代码(位于pastebin Yz79Ck0b)

NOTES: I know I'm not supposed to create a new Mesh each time, but that shouldn't cause this problem, right? 注意:我知道我不应该每次都创建一个新的Mesh,但这不应该引起这个问题,对吗? My IDE is Visual Studio 2008, And I'm using a WIN32 project. 我的IDE是Visual Studio 2008,并且我正在使用WIN32项目。 When I take out the texture and model, I have 0 bytes memory growth. 取出纹理和模型时,内存增长为0字节。

As Sanjit says, you must use delete[] to delete arrays - and as DeadMG says, a RAII container is the best-practice way not to mess up freeing your resource. 正如Sanjit所说,您必须使用delete[]删除数组-正如DeadMG所说,RAII容器是不浪费资源的最佳实践方法。

However, although it's not instantly obvious from the docs, apparently all Direct3d resources (including the IDirect3d9Texture that D3DXCreateTextureFromFile creates) inherit from IUnknown - they're COM objects, in other words. 但是,尽管从文档中并不能立即看出,但是显然所有Direct3d资源 (包括D3DXCreateTextureFromFile创建的IDirect3d9Texture )都从IUnknown继承-换句话说,它们是COM对象。 You need to .Release() COM objects when you're done using them: 使用.Release() COM对象后,您需要.Release() COM对象:

for (int i=0; i<len; i++)
  if (m_Texture[i] != NULL)
    m_Texture[i]->Release();//decrement COM refcount to permit releasing the resource
delete[] m_Texture;
delete[] m_Material;

You probably want to get in the habit of wrapping that recurring logic into a handy RAII container. 您可能想养成将循环逻辑包装到方便的RAII容器中的习惯。 ATL has such a thing built-in, see the MSDN-docs on CComPtr . ATL内置了这样的东西,请参阅CComPtr上的MSDN文档。 Caveat: I've never used it before, but something just like it is a very good idea if you intend to write anything larger than a toy app using COM. 警告:我以前从未使用过它,但是如果您打算使用COM编写比玩具应用程序还要大的东西,那就像一个很好的主意。

m_Material is an array, you need the [] with the delete operator: m_Material是一个数组,需要带删除运算符的[]:

delete[] m_Material;

Also, looks like m_Texture is an array of pointers which are newed. 同样,看起来m_Texture是一个新的指针数组。 D3DXCreateTextureFromFileA(m_Device, d3dxMaterials[i].pTextureFilename, &m_Texture[i]) D3DXCreateTextureFromFileA(m_Device,d3dxMaterials [i] .pTextureFilename,&m_Texture [i])

The proper way of deleting an array of dynamically allocated pointers is: 删除动态分配的指针数组的正确方法是:

    for (int i=0; i<len; i++)
    {
      // first delete memory for each index
      if (m_Texture[i] != NULL)
      {
        delete m_Texture[i];
      }
    }

    // then delete the array
    delete[] m_Texture;

You need to use RAII to handle any resource that needs freeing, and NOT free them manually. 您需要使用RAII处理任何需要释放的资源,而不是手动释放它们。

template<typename T> class self_release_ptr {
    T* ptr;
public:
    self_release_ptr(T* newptr) : ptr(newptr) {}
    self_release_ptr() : ptr(0) {}
    ~self_release_ptr() { if (ptr) ptr->Release(); }
    // etc
};
std::vector<self_release_ptr<texture_type>> m_Texture;

I could be more specific, but would need to see the definition of the classes. 我可以更具体一些,但是需要查看类的定义。

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

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