Sign in to follow this  

Question about IUnknown interface, and COM objects.

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

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
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
No need to reinvent the wheel on this one, use CComPtr if you have access to ATL. If you don't, you can use boost::shared_ptr with a custom deleting function.

Share this post


Link to post
Share on other sites

This topic is 3532 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.

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