Jump to content

Core crash when cancelling server timers


Recommended Posts

  • VIP

When cancelling server timers a core will crash as it is removing a timer from the map and increasing the iterator twice by calling erase() function and afterwards increasing it once again when entering a new cycle of loop. One way of solving this issue is to first collect the timers which must be removed and clean them up at the end. You can also add a simple counter which is increased at each end of the loop's cycle and remove the timer directly from the first loop by specifying the position with the counter itself.

 

Note that this issue seems to appear after upgrading code to C++11 or higher.

 

Replace the following function inside questmanager.cpp:

	void CQuestManager::CancelServerTimers(DWORD arg)
	{
		vector<pair<string, DWORD>> ServerTimersToDelete;

		for (const auto& kv : m_mapServerTimer) {
			if (kv.first.second == arg) {
				LPEVENT event = kv.second;
				event_cancel(&event);
				ServerTimersToDelete.push_back(kv.first);
			}
		}

		// Delete all the required server timers
		for (const auto &timer : ServerTimersToDelete)
			m_mapServerTimer.erase(timer);

		// Clean up
		ServerTimersToDelete.clear();
	}

 

Edited by Sonitex
  • Love 7
Link to post
  • Honorable Member

thanks for fix :)

For me, I wouldn't prefer this. Because you are copying pair. Just use simple iterator like this:

for ( ; it != container.end(); ) {
	if (condition)
		it = container.erase(it);
	else
		++it;
}

 

  • Angry 1

🌛

Link to post
  • VIP
17 minutes ago, Mali61 said:

thanks for fix :)

For me, I wouldn't prefer this. Because you are copying pair. Just use simple iterator like this:

for ( ; it != container.end(); ) {
	if (condition)
		it = container.erase(it);
	else
		++it;
}

 

 

That is the other way I was talking about and you are right, this is much more efficient way of doing it as we are not copying elements to another vector and cleaning them up afterwards. Thank you for the code ;)

  • Love 1
Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

Terms of Use / Privacy Policy / Guidelines / We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.