Sign in to follow this  
grill8

Two quick questions involving COM and self deletion in C++.

Recommended Posts

Hello, I have two quick questions - one involving COM and one about self deletion in C++. Question 1) Say I have the following code:
[source lang = "C++"]
JResource::Release
(
)
{
     --Ref_Cnt;
     ResourceManager::Unaquire(this);
}


The JResource object was created via new, and the resource manager deletes the pointer passed to it (the this) if its ref count is zero. My question is ... after deletion, the object is no longer valid, but the program is still currently in the deleted objects method. Is this ok? What happens if after self deletion you alter member data (this would never happen of course) but out of curiosity, are you overwriting memory? Is this a program flaw or, still valid execution. Question 2) In my resource management I use the following COM methods: 1) Release() 2) AddRef() 3) GetRefCount() I don't require any need for the QueryInterface method and see no reason for it in my design, but is it bad policy to omit it? Am I still technically applying to the COM structure without implementing QueryInterface? I am still a bit shaky as to why exactly it is necessary when a person could simply do without REFIIDS and such and simply validate dynamic_cast on the pointer(s). Thank you for your help. Jeremy

Share this post


Link to post
Share on other sites
Deletion while inside its own member function (or a fucntion called from there etc) is safe as long as afterwards you don't try and access ANY member variables OR call any virtual functions. Basically you shouldn't do anything but return.

I don't recall ever coming across GetRefCount in COM.
QueryInterface is very frequently used in my place of work. Largely we use it through CComQIPtr.
You need to implement that method for the IUnknown Interface. Do you Implement IDispatch instead or something?
Your caller cant always use dynamic_cast because the caller might not even be using C++.
I would suggest implementing the IUnknown interface, including of course the QueryInterface, even if you don't plan on using it.

Share this post


Link to post
Share on other sites
About "delete this"

Actually point #4 has been argued a lot because in most c++ compiler's implementation it's usually OK to read the value of this after calling "delete this". It's still undefined behavior. (or implementation defined behavior, maybe)

Share this post


Link to post
Share on other sites
Thank you both.

I am using AddRef, GetRefCount, and Release to help manage resources in my 3d application by instancing resources. To be honest I have never heard of IDispatch so I am obviously not using it.
It is kind of a pickle because I do not directly need QueryInterface in my resource module but it would be a shame to omit it since the rest of it is using the fundamentals of COM.

If anyone has any suggestions I would love to hear them.

Thanks again,
Jeremy

Share this post


Link to post
Share on other sites
If you don't implement QueryInterface then you can't be deriving from IUnknown, and if you're not deriving from IDispatch, and you're not deriving from something that itself derives from one of those two, then I can't see that you actually even have a COM class at all.
It sounds more like you're simply following COM's design pattern. That's fine as well though.
If you do use COM, I highly recommend using ATL to have it do most of the work for you.

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