C++使用 D3DX9 - 内存大小以 10 MB/秒的速度持续增加 - 为什么?

发布于 2024-10-07 09:20:39 字数 412 浏览 2 评论 0原文

我正在开发 3D 引擎作为学校项目。 我已经安装了虚拟泄漏检测器来消除内存泄漏(并且它有效,因为我不再得到任何这些转储)。 最近,我不小心让我的应用程序运行了 5 分钟...... 我的电脑速度慢得要命。 我通过注释行等找到了这个问题,但我不明白为什么 C++ 在我关闭应用程序之前不会释放我清除的内存。

问题: “内部”内存泄漏,看起来 C++ 仅在应用程序关闭后才删除某些内容

代码: 我的渲染代码(位于pastebin Yz79Ck0b)

注意: 我知道我不应该每次都创建一个新的网格,但这不应该导致这个问题,对吗? 我的IDE是Visual Studio 2008,我使用的是WIN32项目。 当我取出纹理和模型时,内存增长为 0 字节。

I'm working on a 3D engine as school project.
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.
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.

PROBLEM:
"Internal" memory leak, looks like C++ is only deleting something once the app closes

CODE:
My render code (at pastebin Yz79Ck0b)

NOTES:
I know I'm not supposed to create a new Mesh each time, but that shouldn't cause this problem, right?
My IDE is Visual Studio 2008, And I'm using a WIN32 project.
When I take out the texture and model, I have 0 bytes memory growth.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

淡看悲欢离合 2024-10-14 09:20:39

正如 Sanjit 所说,您必须使用 delete[] 来删除数组 - 正如 DeadMG 所说,RAII 容器是不会搞乱资源释放的最佳实践方法。

然而,尽管从文档中看并不明显,但显然所有 Direct3d 资源 (包括IDirect3d9TextureD3DXCreateTextureFromFile 创建)继承自 IUnknown - 换句话说,它们是 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;

您可能希望养成将重复出现的逻辑包装到方便的 RAII 容器中的习惯。 ATL 内置了这样的东西,请参阅 CComPtr。警告:我以前从未使用过它,但如果您打算使用 COM 编写比玩具应用程序更大的东西,那么类似的东西是一个非常好的主意。

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.

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. You need to .Release() COM objects when you're done using them:

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. ATL has such a thing built-in, see the MSDN-docs on CComPtr. 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.

哑剧 2024-10-14 09:20:39

m_Material 是一个数组,您需要带有删除运算符的 []:

delete[] m_Material;

另外,看起来 m_Texture 是一个新的指针数组。
D3DXCreateTextureFromFileA(m_Device, d3dxMaterials[i].pTextureFilename, &m_Texture[i])

删除动态分配的指针数组的正确方法是:

    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;

m_Material is an array, you need the [] with the delete operator:

delete[] m_Material;

Also, looks like m_Texture is an array of pointers which are newed.
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;
眼趣 2024-10-14 09:20:39

您需要使用 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;

我可以更具体,但需要查看类的定义。

You need to use RAII to handle any resource that needs freeing, and NOT free them manually.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文