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!!!)
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.
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...
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.
精彩评论