Microsoft KB Archive/811193

= BUG: You receive a &quot;0xC0000005&quot; error code when a native event is fired or unhooked =

Article ID: 811193

Article Last Modified on 11/13/2007

-

APPLIES TO


 * Microsoft Visual C++ .NET 2002 Standard Edition

-





SYMPTOMS
When you fire the __raise event, or you unhook (__unhook) native events, in an unmanaged application, you may receive the following error message:

Unhandled exception at  in  : 0xC0000005: Access violation reading location

where  and   are values that are specific to your application.



CAUSE
The problem occurs when the constructor of the event source is defined outside the class scope. The event handler list is not initialized in the constructor of the event source in the injected code. Therefore, the access violation occurs when the uninitialized memory is accessed.



WORKAROUND
To work around this problem, use one of the following methods:

Method 1
Define the constructor of the event source in its class scope. To do this, replace the existing code: struct A { __event void Event; A; void bar{} }; //Constructor defined outside class scope. A::A { } with the following code: struct A { __event void Event; A { }; //Contructor defined in class scope. void bar{} };

Method 2
Include the eventHandlerList initialization code in the event source constructor. This assumes that the .cpp file that contains the event source class definition is event.cpp. The injected code that is generated by the compiler can be stored in an intermediate file with help of the /Fx command line compiler option.   At the command prompt, type the following command: cl /Fx event.cpp The injected code that is generated by the compiler is saved in a file with the .mrg.cpp extension. For event.cpp, the file with the injected code is event.mrg.cpp.  In the intermediate file that is generated (event.mrg.cpp), add the following code to the event source constructor:   If the event source class that is named A is defined in a namespace X, add the following code: __eventHandlerList_X_A_Event = 0; to the constructor for A.   If the event source class that is named A is not defined in any namespace, add the following code: __eventHandlerList_A_Event = 0; to the constructor for A. 

Where A is the event source class, X is the namespace for A, and Event is the event identified by the __event keyword in the event source class.  Compile the intermediate file at the command prompt with the following command: cl event.mrg.cpp </li></ol>

<div class="status_section">

STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

<div class="moreinformation_section">

Steps to Reproduce the Behavior
You can reproduce the problem with either of the following two scenarios:

Scenario 1
Define an event source inside a namespace. Additionally, the event source and the event receiver are implemented by the same class or struct. To do this, follow these steps: <ol> In Visual Studio .NET, create a Visual C++ Win32 Console Application project.</li>  In the default .cpp file that is generated for your application, replace the existing code with the following code: namespace X { //Both event source and event receiver struct A { __event void Event; A; void bar{} }; } X::A::A { }
 * 1) include &quot;stdafx.h&quot;

int main {    X::A a;     __hook(&X::A::Event, &a, &X::A::bar, &a); a.Event; __unhook(&X::A::Event, &a, &X::A::bar, &a); } </li> Build and run the application.</li></ol>

You receive the error message that is described in the &quot;Symptoms&quot; section of this article.

Scenario 2
Define an event source and event receiver in two different classes or structs. To do this, follow these steps: <ol> In Visual Studio .NET, create a Visual C++ Win32 Console Application project.</li>  In the default .cpp file that is generated for your application, replace the existing code with the following code: //Event source struct A { __event void Event; A; }; //Event Receiver struct B { void bar; };
 * 1) include &quot;stdafx.h&quot;

void B::bar{} A::A { }

int main {   A a;    B b;    __hook(&A::Event, &a, &B::bar, &b); a.Event; __unhook(&A::Event, &a, &B::bar, &b); } </li> Build and run the application.</li></ol>

You receive the error message that is described in the &quot;Symptoms&quot; section of this article.

The problem occurs because the compiler generates incorrect injected code for the constructor of the event source. To view the injected code that is generated by the compiler, compile the earlier code at the command prompt as follows: cl /Fx event.cpp where the code is saved in event.cpp. The code that is generated by the compiler is available in a file that is named event.mrg.cpp. The event class constructor does not contain the following eventHandlerList initialization code when the event class constructor is defined outside the class scope:   When the event source class that is named A is defined in a namespace X __eventHandlerList_X_A_Event = 0; </li>  When the event source class that is named A is not defined in any namespace __eventHandlerList_A_Event = 0; </li></ul>

Where A is the event source class, X is the namespace for A, and Event is the event that is identified by the __event keyword in the event source class.

Note Similarly the __raise event keyword shows the same behavior that is displayed by the __unhook event keyword.

<div class="references_section">