This topic is now archived and is closed to further replies.

Memory Leak

This topic is 5670 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m using D3D8 to load several mesh objects along with their materials and all are put in the managed resource pool. I''m tracking the memory usage with the task manager. The problem is that when I release all my objects, the task manager doesn''t show any decrease in the memory usage. When I''m reloading the same objects again (without quiting the application), the memory usage grows even further and so on. Is there anything to put into consideration when dealing with managed resources? Is there any way to make sure that a COM object is not being released? Thanks in advance.

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
Is there any way to make sure that a COM object is not being released?

Well, COM objects have a reference count. When that count reaches zero, the object is released.

Here''s a snipet from the IUnKnown interface, from which all COM objects are based:

The IUnknown interface lets clients get pointers to other interfaces on a given object through the QueryInterface method, and manage the existence of the object through the IUnknown::AddRef and IUnknown::Release methods. All other COM interfaces are inherited, directly or indirectly, from IUnknown. Therefore, the three methods in IUnknown are the first entries in the VTable for every interface.

and the release mechanism actually returns the ref count:


Decrements the reference count for the calling interface on a object. If the reference count on the object falls to 0, the object is freed from memory.

ULONG Release(void);

Return Value
Returns the resulting value of the reference count, which is used for diagnostic/testing purposes only.

If IUnknown::AddRef has been called on this object''s interface n times and this is the n+1th call to IUnknown::Release, the implementation of IUnknown::AddRef must cause the interface pointer to free itself. When the released pointer is the only existing reference to an object (whether the object supports single or multiple interfaces), the implementation must free the object.

Note Aggregation of objects restricts the ability to recover interface pointers.

Notes to Callers
Call this function when you no longer need to use an interface pointer. If you are writing a function that takes an in-out parameter, call IUnknown::Release on the pointer you are passing in before copying the out-value on top of it.

I would release my objects like this: (Note: this is very dangerous)

ULONG count = obj.Release();

while(count > 0)
count = obj.Release();

Finally, when you create a new buffer with DX8, it creates a totally new buffer, it doesn''t get the handle to the old one.

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
Same AP:

I''m not used to having the last word... Did everyone who read this understand it?

If Not: DirectX is COM based. COM allows multiple instances of the same object to be passed around (by using the QueryInterface method). The Interfaces, such as D3DDevice, DirectDraw7, DirectInput7, all of the interfaces have IIDs that allow them to be created with QueryInterface incase you want DX7 while using DX8.

To keep track of how many times it has lent itself out for each interface or object, it keeps a reference counter (Think of the Singleton pattern). The object is freed from memory when it has been released enough times to make it zero.

Share this post

Link to post
Share on other sites