SA-MP Forums Archive
Memory Leak/Inefficient timer? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Memory Leak/Inefficient timer? (/showthread.php?tid=537649)



Memory Leak/Inefficient timer? - austin070 - 16.09.2014

I created a cell phone system that has three different types of hanging up.. EndCallForPlayer (for the caller if he wants the call to stop), EndCallForBoth (for both clients), and EndCallFromRing. The timer calls EndCallFromRing, but it appears to make the server lag near the timer's expiration. Is it somehow coded inefficiently?

pawn Код:
forward EndCallFromRing(playerid, recipient);
new ringtimer[MAX_PLAYERS];

public OnPlayerText(playerid, text[])
{
    if(Calling[playerid] == 1)
    {
        if(IsNumeric(text))
        {
            new recip, str[128];
            recip = GetIDFromCell(strval(text));
            if(recip == -1) return SendClientMessage(playerid, COLOR_RED, "(ERROR) {FFFFFF}Invalid phone number.");
           
            format(str, sizeof(str), "(CELL) Receiving call from %d...", PlayerInfo[playerid][CellNumber]);
            SendClientMessage(recip, COLOR_WHITE, str);
            format(str, sizeof(str), "(CELL) You are calling %d...", PlayerInfo[recip][CellNumber]);
            SendClientMessage(playerid, COLOR_WHITE, str);
            ReceivingCall[recip] = PlayerInfo[playerid][CellNumber];
            SendingCall[playerid] = PlayerInfo[recip][CellNumber];
            ringtimer[playerid] = SetTimerEx("EndCallFromRing", 30000, 0, "ii", playerid, recip);
            Calling[playerid] = 0;
           
            return 0;
        }
        else
        {
            SendClientMessage(playerid, COLOR_RED, "(ERROR) {FFFFFF}Invalid phone number.");
            Calling[playerid] = 0;
            return 0;
        }
    }
   return 1;
}


public EndCallFromRing(playerid, recipient)
{
    KillTimer(ringtimer[playerid]);
    SendClientMessage(playerid, COLOR_WHITE, "(CALL) The call has ended.");
    SendClientMessage(recipient, COLOR_WHITE, "(CELL) You have a missed call.");
    SendingCall[playerid] = 0;
    ReceivingCall[recipient] = 0;

    return 1;
}


stock GetIDFromCell(number)
{
    new id = -1;
    foreach(Player, i)
    {
        if(PlayerInfo[i][CellNumber] == number)
        {
            id = i;
        }
    }

    return id;
}
My gut tells me it is from GetIDFromCell.


Re: Memory Leak/Inefficient timer? - Snipa - 16.09.2014

It's very inefficient.

Recode it.


Re: Memory Leak/Inefficient timer? - austin070 - 16.09.2014

Quote:
Originally Posted by Snipa
Посмотреть сообщение
It's very inefficient.

Recode it.
Any advice on what is inefficient specifically?


Re: Memory Leak/Inefficient timer? - Vince - 16.09.2014

I don't see anything inherently wrong with it. It isn't a looping timer so you don't need the variable, nor do you need to kill the timer. But that shouldn't really affect anything.

The GetIDFromCell function can be slightly optimized by immediately returning the playerid once found, rather than continuing the loop.

Pro tip: use the predefined constant INVALID_PLAYER_ID when handling an invalid playerid, rather than -1, 9999 or some other arbitrary value. Seems straightforward, but yet it's very underused.


Re: Memory Leak/Inefficient timer? - austin070 - 16.09.2014

Quote:
Originally Posted by Vince
Посмотреть сообщение
I don't see anything inherently wrong with it. It isn't a looping timer so you don't need the variable, nor do you need to kill the timer. But that shouldn't really affect anything.

The GetIDFromCell function can be slightly optimized by immediately returning the playerid once found, rather than continuing the loop.

Pro tip: use the predefined constant INVALID_PLAYER_ID when handling an invalid playerid, rather than -1, 9999 or some other arbitrary value. Seems straightforward, but yet it's very underused.
I do need the variable in order to end the ring elsewhere. Otherwise, thanks for the tip.