简体   繁体   中英

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. 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.

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.

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;

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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