Jump to content
×
×
  • Create New...

Core crash when cancelling server timers


Recommended Posts

  • Premium

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
  • Metin2 Dev 1
  • Love 8
Link to comment
Share on other sites

  • 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 comment
Share on other sites

  • Premium
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 comment
Share on other sites

  • 1 year later...

this is the best fix if someone need it

	void CQuestManager::CancelServerTimers(DWORD arg)
	{
		itertype(m_mapServerTimer) it = m_mapServerTimer.begin();
   
		for (; it != m_mapServerTimer.end(); )
		{
			if (it->first.second == arg)
			{
				LPEVENT event = it->second;
				event_cancel(&event);
				m_mapServerTimer.erase(it++);
			}
			else
				++it;
		}
	}

 

  • Love 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.


  • Activity

    1. 36

      Destroy Item System

    2. 1

      Brillances

    3. 0

      Fusion.org is Looking for Staff Members

    4. 1

      Brillances

    5. 3

      ELONIA2 | PVM-MEDIUM | 11 LANGUAGES | INTERNATIONAL | A NEW STORY

    6. 4

      Spider Queen not spawining in spider dungeon v1

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.