LASTEXPRESS: Fix leak and corruption of event handlers function pointers

- Add better error handling
 - Properly delete previous event handlers when setting new ones or restoring from backup
This commit is contained in:
Littleboy 2011-06-22 02:47:51 -04:00
parent 434a2e66b1
commit 85bb5ef45e

View File

@ -288,22 +288,34 @@ void LastExpressEngine::handleSoundTimer() {
/// Event Handling
///////////////////////////////////////////////////////////////////////////////////
void LastExpressEngine::backupEventHandlers() {
if (_eventMouseBackup != NULL || _eventTickBackup != NULL)
error("[LastExpressEngine::backupEventHandlers] backup event handlers are already set");
_eventMouseBackup = _eventMouse;
_eventTickBackup = _eventTick;
}
void LastExpressEngine::restoreEventHandlers() {
if (_eventMouseBackup == NULL || _eventTickBackup == NULL)
error("LastExpressEngine::restoreEventHandlers: restore called before backing up the event handlers!");
error("[LastExpressEngine::restoreEventHandlers] restore called before backing up the event handlers");
// Cleanup previous event handlers
SAFE_DELETE(_eventMouse);
SAFE_DELETE(_eventTick);
_eventMouse = _eventMouseBackup;
_eventTick = _eventTickBackup;
_eventMouseBackup = NULL;
_eventTickBackup = NULL;
}
void LastExpressEngine::setEventHandlers(EventHandler::EventFunction *mouse, EventHandler::EventFunction *tick) {
// Cleanup previous event handlers
delete _eventMouse;
delete _eventTick;
if (_eventMouse != _eventMouseBackup)
SAFE_DELETE(_eventMouse);
if (_eventTick != _eventTickBackup)
SAFE_DELETE(_eventTick);
_eventMouse = mouse;
_eventTick = tick;