Skip to content

Commit

Permalink
Fix (eventual) timer crash when adding timer from inside timer
Browse files Browse the repository at this point in the history
  • Loading branch information
Cheaterman committed Apr 23, 2022
1 parent 0bbde39 commit a173810
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
31 changes: 15 additions & 16 deletions src/pysamp/timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ Timer& Timer::operator=(const Timer& timer)
return *this;
}

bool Timer::operator==(const Timer& timer)
{
return timer.id == id;
}

Timer* Timer::from_args(PyObject *args, PyObject *arguments)
{
PyObject* function;
Expand Down Expand Up @@ -123,27 +128,21 @@ void TimerManager::process_timers(unsigned int current_tick)
if(disabled)
return;

for(auto timer = timers.begin(); timer != timers.end();)
for(unsigned int i = 0; i < timers.size(); ++i)
{
if(timer->is_pending_deletion())
{
++timer;
continue;
}
Timer& timer = timers[i];

int id = timer->get_id();
bool repeating = timer->is_repeating();
if(timer.is_pending_deletion())
continue;

if(
timer->process(current_tick)
&& !repeating
timer.process(current_tick)
&& !timer.is_repeating()
)
{
timer = timers.erase(timer);
continue;
}

++timer;
timers.erase(
std::remove(timers.begin(), timers.end(), timer),
timers.end()
);
}

for(auto timer = timers.begin(); timer != timers.end();)
Expand Down
2 changes: 2 additions & 0 deletions src/pysamp/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define PYTIMER_H

#include <Python.h>
#include <algorithm>
#include <deque>

#include "sampgdk.h"
Expand All @@ -18,6 +19,7 @@ class Timer
);
~Timer();
Timer& operator=(const Timer& timer);
bool operator==(const Timer& timer);

static Timer* from_args(PyObject *args, PyObject *arguments);
bool process(unsigned int current_tick);
Expand Down

0 comments on commit a173810

Please sign in to comment.