Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assertion failure in EventDispatcher::TriggerEvents #45

Closed
viciious opened this issue Sep 1, 2019 · 5 comments
Closed

Assertion failure in EventDispatcher::TriggerEvents #45

viciious opened this issue Sep 1, 2019 · 5 comments

Comments

@viciious
Copy link
Contributor

viciious commented Sep 1, 2019

The following line produces an assertion failure in debug build in MSVC:

	for (auto it = begin; it != end; ++it)

Further down the stack:

	_Vector_const_iterator& operator++()
		{	// preincrement
 #if _ITERATOR_DEBUG_LEVEL != 0
		const auto _Mycont = static_cast<const _Myvec *>(this->_Getcont());
		_STL_VERIFY(_Ptr, "can't increment value-initialized vector iterator");
		_STL_VERIFY(_Ptr < _Mycont->_Mylast, "can't increment vector iterator past end");
 #endif /* _ITERATOR_DEBUG_LEVEL != 0 */

The _Mycont pointer is null

@viciious
Copy link
Contributor Author

viciious commented Sep 1, 2019

In this particular case, a button click event hides the document which triggers another event, in which a listener de-attaches itself right in the ProcessEvent call and that confuses the iterator.

@viciious
Copy link
Contributor Author

viciious commented Sep 1, 2019

Changing the loop to read:

	for (auto it = begin, next = end; it != end; it = next)
	{
		next = std::next(it, 1);
		it->listener->ProcessEvent(event);
	}

fixes the assertion failure :)

@mikke89
Copy link
Owner

mikke89 commented Sep 1, 2019

Yeah, I think the problem is that when adding or removing listeners, the iterators will be invalidated. Could you test the changes I committed?
The commit should help avoid invalidated iterators, but may result in a skip or repeat of a listener invocation.

@viciious
Copy link
Contributor Author

viciious commented Sep 2, 2019

I think the best solution here would be to copy the slice of the original array to a temporary array and iterate over that.

mikke89 added a commit that referenced this issue Sep 12, 2019
…ether event is propagating between each dispatch.
@mikke89
Copy link
Owner

mikke89 commented Sep 12, 2019

Okay, I guess I'm fine with the approach, at least until I see any real-world problems with it :)

@mikke89 mikke89 closed this as completed Sep 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants