Jump to content
  • Advertisement
Sign in to follow this  
derek7

IUnknown::AddRef

This topic is 4898 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

Advertisement
MSDN
it's only a reference counter, AddRef increase by one a 32 bit internal member, Release decrease by one. When no reference from outside the object is deleted from memory

Share this post


Link to post
Share on other sites
Take a look at the DirectX SDK documentation at:

The DirectX Software Development Kit ->
DirectX Programming Guide ->
Using COM


IUnknown is the base class for all COM objects. All COM objects are "reference counted".

AddRef adds 1 to the reference count for an object. When you call Release() on an object, 1 is subtracted from its reference count; the object is only destroyed when its reference count is at 0.

in pseudo-code:


class IUnknown
{
public:
virtual AddRef() = 0;
virtual Release() = 0;
...
}

class IDirect3DBlah : public IUnknown
{
public:
AddRef();
Release();
...
private:
int m_refCount;
}

IDirect3DBlah::AddRef()
{
m_refCount++;
}

IDirect3DBlah::Release()
{
m_refCount--;
if (m_refCount == 0)
{
delete this object
}
}



Calling AddRef on an object directly can be useful to prevent it from being destroyed unexpectedly when you don't have control over the code which created the interface.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!