The event context that the VISA driver generates when an event occurs is a data object that contains the information about the event. Because it is more than just a simple variable, memory allocation and deallocation becomes important.

Event Context with the Queuing Mechanism

When you use the queuing mechanism, the event context is returned when you call viWaitOnEvent(). The VISA driver has created this data structure, but it cannot destroy it until you tell it to. For this reason, in VISA you call viClose() on the event context so the driver can free the memory for you. Always remember to call viClose() when you are done with the event.

If you know the type of event you are receiving, and the event does not provide any useful information to your application other than whether it actually occurred, you can pass VI_NULL as the outEventType and eventContext parameters as shown in the following example:
status = viWaitOnEvent(gpibSesn, VI_EVENT_SERVICE_REQ, 5000, VI_NULL, VI_NULL);

In this case, VISA automatically closes the event data structure rather than returning it to you. Calling viClose() on the event context is therefore both unnecessary and incorrect because VISA would not have returned the event context to you.

Event Context with the Callback Mechanism

In the case of callbacks, the event is passed to you in a function, so the VISA driver has a chance to destroy it when the function ends. This has two important repercussions. First, you do not need to call viClose() on the event inside the callback function. Indeed, calling this operation on the event could lead to serious problems because VISA will access the event (to close it) when your callback returns. Secondly, the event itself has a life only as long as the callback function is executing. Therefore, if you want to keep any information about the event after the callback function, you should use viGetAttribute() to retrieve the information for storage. Any references to the event itself becomes invalid when the callback function ends.