Jump to content
  • Advertisement
Sign in to follow this  
grill8

Question about IUnknown interface, and COM objects.

This topic is 3715 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
Advertisement
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
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!