Fixed a potential crash in EventDispatcher, based on PR #6160

This PR is based on #6160