Memory Usage
The event function is one of the very few functions in WMX3 that require dynamic access to memory in the RTX space. The dynamic memory allocation occurs when an event is defined for a particular event ID for the first time since starting the engine. Once an event is defined, the memory allocated to store the event data remains even after the event is removed. When a new event is defined for that event ID, the allocated memory is reused and no additional dynamic memory access occurs. If there is not enough memory in RTX space, the SetEvent function will return an error and the event will not be defined.
To prevent issues with memory allocation, call SetEvent for all event IDs that will be used by the application. Then, clear all events with ClearAllEvent (the allocated memory will remain, so no dynamic memory access occurs when an event is later defined). The event input and event output can be set to None.
For additional information, see Function Calls Related to Memory Allocation.
Memory Allocation Example
The following example code allocates memory for all event IDs. After this code is executed without any error, no event functions will require dynamic allocation of memory until the engine is restarted. To allocate all event memory beforehand, execute this code during the initialization of the user application.
EventApiEventInput ein;
EventApiEventOutput eout;
int i, err, pid;
ein.inputFunction = EventApiEventInput::EventApiEventInputType::None;
eout.outputFunction = EventApiEventOutput::EventApiEventOutputType::None;
for (i = 0; i < constants::maxEvents; i++) {
//Define event
err = wmxlib_EventControl->SetEvent(&pid, &ein, &eout, i);
if (err == EventErrorCode::EventInputDataAllocateFailed
|| err == EventErrorCode::EventOutputDataAllocateFailed) {
//Error: Memory allocation failed
return err;
}
else if (err != ErrorCode::None) {
//Error: Other error
return err;
}
}
//Clear all events
err = wmxlib_EventControl->ClearAllEvent();
if (err != ErrorCode::None) {
//Error: Error clearing events
return err;
}