Sign in to follow this  
grill8

Question about IUnknown interface, and COM objects.

Recommended Posts

grill8    148
Hello, I have a question about the DirectX IUnknown interface, and COM objects. Ok, so I have a template function as follows, which safe releases a COM object pointer.
template
<
	typename tyPtr
>
inline
void
JSafeRelease
(
	tyPtr Ptr
)
{
	if(Ptr){Ptr->Release(); *Ptr = NULL;}
}


My question is ... is is possible to do away with the template part and simply pass an IUnknown pointer to the function? I know that when a ref count becomes 0 the object resources are destroyed and the interface becomes invalid etc. but ... behind the scenes if the object ref count becomes 0 via an IUnknown pointer does that still behave correctly? I am assuming that it COULD via virtual functions etc. but do ALL IUnknown pointers behave properly in this manner? The new function would look something like:
inline
void
JSafeRelease
(
	IUnknown*& rPtr
)
{
	if(rPtr){rPtr->Release(); rPtr = NULL;}
}


Thank you for your help. Jeremy [Edited by - grill8 on May 15, 2008 10:17:30 PM]

Share this post


Link to post
Share on other sites
AnAss    98
In DirectX: Yes. No DX interface is multiply inherited, so you won't have any issues.

In general: No. Some COM objects have multiple inheritance, and the compiler will bitch about ambiguous conversions to IUnknown.

By the way, google CComPtr.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this