You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
RemoveMapChangeTimers is iterating a vector while possibly removing from said vector, the current code does not shift the iterator correctly when an entry is removed and the iteration can skip entries. I would suggest to rewrite the entire function to use std::erase_if to kill all the active timers instead of manually iterating and calling KillTimer, this both fixes the issue and improves performance as you don't have to shift the entire vector every iteration.
Test plugin:
Run a dedicated server with the plugin loaded, load any map and then run start_timer_test
usingCounterStrikeSharp.API;usingCounterStrikeSharp.API.Core;usingCounterStrikeSharp.API.Modules.Timers;publicclassTestTimers:BasePlugin{publicoverridestringModuleName=>"Timer Tests";publicoverridestringModuleDescription=>"";publicoverridestringModuleAuthor=>"Poggu";publicoverridestringModuleVersion=>"0.0.1";publicboolMapChangeStarted=false;publicboolMapChanged=false;publicvoidTimerHandler(inttimerNum){Console.WriteLine($"Timer{timerNum} Tick");if(MapChanged){Console.ForegroundColor=ConsoleColor.Red;Console.WriteLine("[!] Test Failed!");Console.ResetColor();MapChanged=false;}}publicoverridevoidLoad(boolhotReload){Console.WriteLine($"{ModuleName} loaded successfully!");AddCommand("start_timer_test","",(player,info)=>{/* Test Preamble */Console.WriteLine("\n\n\n\n----------------------------------------------------------------------");Console.WriteLine("Starting 5 timers, disabling server hibernation and ALL LOG CHANNELS");Server.ExecuteCommand("log_verbosity Console off; log_verbosity - off; sv_hibernate_when_empty 0;");Console.WriteLine("Changing map in 10 seconds, no timers **should** run after map change.\n----------------------------------------------------------------------\n\n\n");AddTimer(15.0f,()=>{Console.WriteLine("\n\n---------\nChanging map\n---------\n\n\n");Server.ExecuteCommand("map de_dust2");MapChangeStarted=true;});/* Test Timers */AddTimer(3.0f,()=>{TimerHandler(1);},TimerFlags.STOP_ON_MAPCHANGE|TimerFlags.REPEAT);AddTimer(3.0f,()=>{TimerHandler(2);},TimerFlags.STOP_ON_MAPCHANGE|TimerFlags.REPEAT);AddTimer(3.0f,()=>{TimerHandler(3);},TimerFlags.STOP_ON_MAPCHANGE|TimerFlags.REPEAT);AddTimer(3.0f,()=>{TimerHandler(4);},TimerFlags.STOP_ON_MAPCHANGE|TimerFlags.REPEAT);AddTimer(3.0f,()=>{TimerHandler(5);},TimerFlags.STOP_ON_MAPCHANGE|TimerFlags.REPEAT);});RegisterListener<Listeners.OnMapStart>(mapName =>{Console.WriteLine($"Map {mapName} loaded!");if(MapChangeStarted)MapChanged=true;});}}
The text was updated successfully, but these errors were encountered:
RemoveMapChangeTimers
is iterating a vector while possibly removing from said vector, the current code does not shift the iterator correctly when an entry is removed and the iteration can skip entries. I would suggest to rewrite the entire function to usestd::erase_if
to kill all the active timers instead of manually iterating and calling KillTimer, this both fixes the issue and improves performance as you don't have to shift the entire vector every iteration.https://github.com/roflmuffin/CounterStrikeSharp/blob/6349c11d07af8ba95d280e4a0f994ccb46053ef5/src/core/timer_system.cpp#L173C5-L179C6
Test plugin:
Run a dedicated server with the plugin loaded, load any map and then run
start_timer_test
The text was updated successfully, but these errors were encountered: