The information in this article applies to:
SYMPTOMSUsing smart pointers with Visual C++ version 5.0, generated by including a type library with #import, it is possible to experience an unhandled exception error in your application. A common symptom is having near identical code for an object model, such as Collaboration Data Objects (CDO) that works fine in Visual Basic, but not from C++. CAUSEReference counting is being handled improperly. RESOLUTIONWhen reassigning a smart pointer it is important to first set the pointer to NULL. This causes the smart pointers implementation to properly release the previous object before assigning the pointer to the new object. This occurs when you compile and link with Visual C++ 6.0. MORE INFORMATIONSteps to Reproduce BehaviorThis problem can appear in any call in which a Component Object Model (COM) smart pointer is reused. The following example reproduces the problem:
The preceding code fails on the following line because you are now
reassigning the smart pointer without first releasing the underlying
object:
For the preceding code to work, that line should be replaced by the
following two lines of code:
It is also a good idea to set the pointers to NULL at the end of your code.
This must be done if you are going to call the CoUninitialize function in
the same scope as the smart pointers.
REFERENCESInside COM, Dale Rogerson, Microsoft Press, pg. 218. Additional query words:
Keywords : kbole kbCDO121 kbCOMt kbMsg kbVC kbGrpMsg |
Last Reviewed: November 8, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |