PRB: Access Violation with CDO (1.x) in Multi-threaded COM Server (236577)



The information in this article applies to:

  • Collaboration Data Objects (CDO) 1.1
  • Collaboration Data Objects (CDO) 1.2
  • Collaboration Data Objects (CDO) 1.21

This article was previously published under Q236577

SYMPTOMS

When using CDO (1.1, 1.2, 1.21) in a multi-threaded COM server, an access violation may occur on releasing the last object hosted by the server.

CAUSE

CDO (1.1, 1.2, 1.21) uses messaging application programming interface (MAPI), and the first thread that uses MAPI in a process causes certain initialization actions to occur. These particular initialization actions do not reoccur when other threads use MAPI. When the original thread terminates, specific de-initialization actions happen within the server process, which may lead to problems with the other threads using MAPI.

For example, the following scenario illustrates how an access violation can occur:

A Microsoft Visual Basic 6.0 ActiveX EXE server has a class that creates a CDO (1.1, 1.2, 1.21) session in its initialize event and releases it in its terminate event. A client application creates an instance (A) of this class and then a second instance (B). The client releases instance A and then releases instance B. This leads to the de-initialization of MAPI and the shutdown of the server process. During these activities an access violation occurs.

RESOLUTION

There are at least three ways in which you can avoid this problem:
  • Take the creation/destruction of the CDO (1.1, 1.2, 1.21) object out of the class initialize/terminate. Give the class a method which the client calls explicitly to initialize the CDO session and a corresponding method that releases the CDO (1.1, 1.2, 1.21) session.
  • Have the class create a dummy object provided by a DLL with a COM threading model of "single" that creates a CDO (1.1, 1.2, 1.21) session before the main class initializes its own CDO (1.1, 1.2, 1.21) session. This forces MAPI to be initialized first on the primary single threaded apartment (STA) of the process rather than on one of the secondary STAs.
  • Release the objects in the reverse order in which they were created.

Modification Type:MinorLast Reviewed:3/4/2004
Keywords:kbMsg kbprb KB236577