[Tutorial] New Timer Bug
#8

Quote:
Originally Posted by Emmet_
Посмотреть сообщение
This is not a bug..

It happens on any programming language. If you create a timer and then create another one and store the ID in the same variable, then it will be inaccessible, which is therefore called a memory leak because the first timer has no reference.

A fix is pretty simple: just use "-1" as an indication that the timer is not running.

pawn Код:
#define INVALID_TIMER_ID (-1)

new
    g_iTimerID[MAX_PLAYERS]
;

public OnPlayerConnect(playerid) {
    g_iTimerID[playerid] = INVALID_TIMER_ID;
}

public OnPlayerDisconnect(playerid, reason) {

    if (g_iTimerID[playerid] != INVALID_TIMER_ID) {
        KillTimer(g_iTimerID[playerid]);
    }
}

CMD:timer(playerid, params[]) {
   
    if (g_iTimerID[playerid] != INVALID_TIMER_ID)
        KillTimer(g_iTimerID[playerid]);
   
    else
        g_iTimerID[playerid] = SetTimerEx("...", 1500, false, "d", playerid);
}
What I said:

Quote:

It is a programming mistake; you can call it a bug, error, whatever; I call it a bug
Simplest explanation for newbies and new programmers. I created this topic because I saw many servers and programmers with this problem.

Thanks for feedback, +REP
Reply


Messages In This Thread
New Timer Bug - by Ygzeb - 22.11.2014, 00:24
Re: New Timer Bug - by Abagail - 22.11.2014, 00:56
Re: New Timer Bug - by JaKe Elite - 22.11.2014, 00:59
Answer - by Ygzeb - 22.11.2014, 01:45
Re: New Timer Bug - by Quickie - 22.11.2014, 04:06
Answer - by Ygzeb - 22.11.2014, 04:40
Re: New Timer Bug - by Emmet_ - 22.11.2014, 05:31
Answer - by Ygzeb - 22.11.2014, 06:21

Forum Jump:


Users browsing this thread: 6 Guest(s)