Robben_Ford_Fan_boy
Robben_Ford_Fan_boy

Reputation: 8720

Member variable pointers to COM objects

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!!!)

Upvotes: 1

Views: 144

Answers (3)

AndersK
AndersK

Reputation: 36082

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...

Upvotes: 2

Stephen Nutt
Stephen Nutt

Reputation: 3306

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.

Upvotes: 2

sharptooth
sharptooth

Reputation: 170489

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.

Upvotes: 5

Related Questions