C++使用 D3DX9 - 内存大小以 10 MB/秒的速度持续增加 - 为什么?
我正在开发 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如 Sanjit 所说,您必须使用
delete[]
来删除数组 - 正如 DeadMG 所说,RAII 容器是不会搞乱资源释放的最佳实践方法。然而,尽管从文档中看并不明显,但显然所有 Direct3d 资源 (包括
IDirect3d9Texture
,D3DXCreateTextureFromFile
创建)继承自IUnknown
- 换句话说,它们是 COM 对象。您需要.Release()
COM当您使用完对象后,您可能希望养成将重复出现的逻辑包装到方便的 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
thatD3DXCreateTextureFromFile
creates) inherit fromIUnknown
- they're COM objects, in other words. You need to.Release()
COM objects when you're done using them: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.m_Material 是一个数组,您需要带有删除运算符的 []:
另外,看起来 m_Texture 是一个新的指针数组。
D3DXCreateTextureFromFileA(m_Device, d3dxMaterials[i].pTextureFilename, &m_Texture[i])
删除动态分配的指针数组的正确方法是:
m_Material is an array, you need the [] with the delete operator:
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:
您需要使用 RAII 来处理任何需要释放的资源,而不是手动释放它们。
我可以更具体,但需要查看类的定义。
You need to use RAII to handle any resource that needs freeing, and NOT free them manually.
I could be more specific, but would need to see the definition of the classes.