Article ID: 811193 - View products that this article applies to.
When you fire the __raise event, or you unhook (__unhook) native events, in an unmanaged application, you may receive the following error message:
where virtual address and executable are values that are specific to your application.
Unhandled exception at virtual address in executable: 0xC0000005: Access violation reading location virtual address
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.
To work around this problem, use one of the following methods:
Method 1Define the constructor of the event source in its class scope. To do this, replace the existing code:
with the following code:
Method 2Include 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.
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.
Steps to Reproduce the Behavior
You can reproduce the problem with either of the following two scenarios:
Scenario 1Define 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:
Scenario 2Define an event source and event receiver in two different classes or structs. To do this, follow these steps:
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:
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:
Note Similarly the __raise event keyword shows the same behavior that is displayed by the __unhook event keyword.
For additional information about native event handling keywords and attributes, see the following MSDN Web site:
Event Handling Keywords