Skip to main content
gamedev.net
🔒 Locked

Class deallocation

Started by mdfmKoRn Dec 22, 2001 at 1:41 PM 2 replies 300+ views
Original Post
mdfmKoRn
mdfmKoRn
Am I right in assuming that, for example, if you declared a LPDIRECTX8 object or whatever inside another object, (say class Game) the memory will be free when the encapsulating object''s destructor is called? ---- Herb M. (mdfmKoRn) s3202@attbi.com
Oluseyi
Oluseyi
Not if you explicitly allocated the memory.

DirectX interfaces are COM objects. You should always call Release() on them.

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!
Shannon Barber
Shannon Barber
quote:
Original post by mdfmKoRn
Am I right in assuming that, for example, if you declared a LPDIRECTX8 object or whatever inside another object, (say class Game) the memory will be free when the encapsulating object''s destructor is called?



No. You must write code to make this happen.
If the object is allocated on the stack, it will be freed automatically. If you use new, you must delete, if you new[] you must delete[], if you create a COM object you must call ->Release()

If you want them to automatically be released, you need to use smart pointers.

  
#include <ATLBase.h>

class MyClass
{
CComPtr<IDirect3D8> m_spD3D8;

HRESULT Init()
{
if(NULL==(m_spD3D8.p=Direct3DCreate8(D3D_SDK_VERSION)))
return(E_FAIL);

}
};

The CComPtr template will call ->Release when the instance of MyClass is destroyed.
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.