views:

67

answers:

3

Hi Folks, Is there any problem with keeping member variable pointer refernces to COM objects and reussing the reference through out the class in C++.

Is anybody aware of a reason why you would want to call .CreateInstance every time you wanted a to use the COM object i.e. you were getting a fresh instance each time.

I cannot see any reason who you would want to do this,

Thanks,

(No is an acceptable answer!!!)

+4  A: 

It depends on what you really want.

If you need the same object every time you have to keep a pointer to it. If you need a new object every time (for whatever reason) you have to create a new instance each time. If you don't care keeping the object is preferable because calls to CoCreateInstance() are relatively expensive.

sharptooth
I would prefer to keep a reference but am just curious as to why you would want to create new instances each time
David Relihan
@drelihan: The COM object could be non-reusable because of how it works internally.
sharptooth
+1  A: 

I would say it depends on what the COM object is and how you use it. It is generally fine to reuse an ADO connection, but if you leave it in a dirty state then you may encounter odd behavior when you reuse it. Some COM object may have a re-initialize or clear method you can call to reset them back to a clean state.

Stephen Nutt
OK - that makes sense
David Relihan
+1  A: 

There is no general rule in this case because there are a number of variables that decide whether it is a good idea or not.

First: If you own the COM objects in question i.e. have source code and control over how they are used, then yes its perfectly safe.

If COM objects are 3rd party COM objects sometimes crappy code in them may force you to "createInstance" on them every time you use them - out of necessity (and self preservation).

If the COM object is acting as a proxy object you may need to create them every time you use them because of stuff behind the scene i.e. other clients using the same object.

there are more situations, but to summarize: it depends...

Anders K.
So other clients (other COM executables) also using the COM object would be a valid reason for creating an instance each time?
David Relihan
I've been forced to do this mainly when the COM object was poorly designed.
Anders K.
When doing this, is there any issue with having a member variable pointer to the COM object and a method which calls CreateInstance on the Member variable and returns a reference to it? That way I can just substitute my method for any references to the member var without having cluncky CreateInstances littered around the code
David Relihan
you don't need a member variable to the com object if you call createInstance everytime if you instead of returning a reference, return a smart pointer. you can have in your method just a local variable which you use to create the object, then just return the smart pointer thus giving ownership to whoever called the method.
Anders K.
Cool - thanks for that Anders
David Relihan