Fix a crash when deleting elements during event handling.