How to pass IDispatch pointer and avoid an access violation (133042)
The information in this article applies to:
- The Microsoft Foundation Classes (MFC), when used with:
- Microsoft Visual C++, 32-bit Editions 2.0
- Microsoft Visual C++, 32-bit Editions 2.1
- Microsoft Visual C++, 32-bit Editions 2.2
- Microsoft Visual C++, 32-bit Editions 4.0
- Microsoft Visual C++, 32-bit Editions 4.1
- Microsoft Visual C++, 32-bit Enterprise Edition 5.0
- Microsoft Visual C++, 32-bit Professional Edition 5.0
- Microsoft Visual C++, 32-bit Enterprise Edition 6.0
- Microsoft Visual C++, 32-bit Professional Edition 6.0
- Microsoft Visual C++, 32-bit Learning Edition 6.0
This article was previously published under Q133042 SUMMARY
In some cases, you may want an OLE Automation Local-Server to pass its
IDispatch pointer to another Local-Server through an automation method.
However, the pointer will become invalid after the method in the second
server returns, so an Access Violation will occur if the second server
tries to use the pointer later.
To maintain the integrity of the pointer, you need to call AddRef on the
IDispatch pointer within the second server, as described in the following
rule from the OLE SDK documentation:
If a local copy of an interface pointer is made from an existing global
interface pointer, the local copy must be independently reference-
counted. This separate reference count is necessary because the global
copy can be destroyed while the local copy is in use.
REFERENCES- Visual C++ Books Online Contents\OLE 2.0 SDK\Chapter 6 Component
Object Interfaces and Functions \Iunknown Interface\Reference Counting
Rules.
- OLE 2 Programmers Reference, Volume 1, pages 191-195.
- Inside OLE 2, pages 83-90.
Modification Type: | Major | Last Reviewed: | 4/28/2005 |
---|
Keywords: | kbAutomation kbhowto kbServer KB133042 kbAudDeveloper |
---|
|