COM Smart Pointer Trap

xiaoxiao2021-03-06  73

Recently written in a MSXML4.0 packaging class, implement some practical features for your project. Using COM's smart pointer, found that there is some concerns that use smart pointers. The maximum trap of the smart pointer is the so-called it can be destroyed automatically. We know, the smart pointer provides a stack object management pile on object method, using the survival cycle of the object on the stack to control the survival cycle of the heap object, such as we declare a CComptr object in a large bracket, when this big At the end of brackets, it automatically calls Release and releases yourself.

Since it takes the COM's smart pointer, you can't mention the Coinitialize (ex) this function, which is used to initialize the COM library and add the current thread to a department. Every Coinitialize should have a Couninitialize corresponding to it, which also brings a problem. There are some functions in my class to return a ixmldomnodeptr, so I made it a member variable, which is specifically used to return, so that you will be constructed in a function each time; but the program is running an error; in the function A ixmldomnodeptr and returns it but no problem. After examination, it was because I didn't call Couninitialize in my destructure function, but in another function closedocument called it, causing Couninitialize before the class object destroyed, so regardless of its explicit or implicit (Automatic) Calling Release, will fail because all COM resources have been recycled. Therefore, there is the following advice: *************************************************** *********************** Advice: Please ensure that all smart pointers have been released before calling CounInitialize, and must be manually explicitly released. *********************************************************** *******************

转载请注明原文地址:https://www.9cbs.com/read-92676.html

New Post(0)