Run time error 7: "Stack underflow"
#1

The following function called under OnGameModeInit causes a big problem, when I compile I got no warnings but when I start up my server this appears in server log after the function is called:
Quote:

[19:15:29] Script[gamemodes/BCL-RP.amx]: Run time error 7: "Stack underflow"

pawn Code:
stock LoadSQLPickups()
{
    new Field[64];
    new Data[512];
    new sql[80];
    format(sql, sizeof(sql), "SELECT COUNT(*) FROM pickups");
    mysql_query(sql);
    mysql_store_result();
    mysql_fetch_row(Data);
    totalpickups = strval(Data);
    mysql_free_result();
    for (new idx=1; idx<=totalpickups; idx++)
    {
      format(sql, sizeof(sql), "SELECT * FROM pickups WHERE id=%d", idx);
      mysql_query(sql);
      mysql_store_result();
      if (mysql_num_rows() > 0)
      {
        mysql_fetch_row(Data);

          mysql_fetch_field_row(Field,"message");
          strmid(PickupInfo[idx][puMessage],Field,0,strlen(Field),128);

          mysql_fetch_field_row(Field,"type");
          PickupInfo[idx][puType] = strval(Field);
          mysql_fetch_field_row(Field,"model");
          PickupInfo[idx][puModel] = strval(Field);

          mysql_fetch_field_row(Field,"x");
          PickupInfo[idx][puX] = floatstr(Field);
          mysql_fetch_field_row(Field,"y");
          PickupInfo[idx][puY] = floatstr(Field);
          mysql_fetch_field_row(Field,"z");
          PickupInfo[idx][puZ] = floatstr(Field);

          mysql_free_result();
          CreatePickup(PickupInfo[idx][puModel], PickupInfo[idx][puType], PickupInfo[idx][puX], PickupInfo[idx][puY], PickupInfo[idx][puZ]);
        }
    }
    printf("%d pickups loaded from DB", totalpickups);
    return 1;
}
I've debbuged it and here is the buggy part:
pawn Code:
mysql_fetch_field_row(Field,"message");
strmid(PickupInfo[idx][puMessage],Field,0,strlen(Field),128);
This can help:
pawn Code:
enum puInfo
{
    Float: puX,
    Float: puY,
    Float: puZ,
    puType,
    puModel,
    puMessage[128]
}; new PickupInfo[MAX_PICKUP+1][puInfo];
Any idea? This warning fucks up all my script...
Reply
#2

Try increase the array.

new Field[128];
Reply
#3

pawn Code:
mysql_fetch_field_row(PickupInfo[idx][puMessage], "message");
There's no need to strmid it.

pawn Code:
stock LoadSQLPickups()
{
    new
        x,
        totalPickups,
        resultString[64]; // Increase if needed.

    mysql_query("SELECT * FROM pickups");
    mysql_store_result();
   
    totalPickups = mysql_num_rows();
   
    while(mysql_retrieve_row()) {
        mysql_get_field("id", result);
        x = strval(result);
       
        mysql_get_field("message", PickupInfo[x][puMessage]);
       
        mysql_get_field("type", result);
        PickupInfo[x][puType] = strval(result);
       
        mysql_get_field("model", result);
        PickupInfo[idx][puModel] = strval(result);
       
        mysql_get_field("x", result);
        PickupInfo[idx][puX] = floatstr(result);
       
        mysql_get_field("y", result);
        PickupInfo[idx][puY] = floatstr(result);
       
        mysql_get_field("z", result);
        PickupInfo[idx][puZ] = floatstr(result);
       
        CreatePickup(PickupInfo[x][puModel], PickupInfo[x][puType], PickupInfo[x][puX], PickupInfo[x][puY], PickupInfo[x][puZ]);
    }
   
    mysql_free_result();
   
    return printf("%d pickups loaded from DB", totalPickups);
}
Reply
#4

Wow, didn't even see that Calgon. Here I was typing up a large post when it wasn't needed

However, something else I noticed in your code. It seems as if you're wasting an entire row of memory in your array. Array indexes start at 0 and end at size - 1. So you should really change your for loop to something like below else you're just wasting memory. It may only be a matter of bytes now, but you could possibly keep this "habit" and waste a significant amount of memory in larger projects.

From:
for (new idx = 1; idx<=totalpickups; idx++)

To:
for (new idx = 0; idx< totalpickups; idx++)
Reply
#5

Quote:
Originally Posted by Simon
View Post
Wow, didn't even see that Calgon. Here I was typing up a large post when it wasn't needed

However, something else I noticed in your code. It seems as if you're wasting an entire row of memory in your array. Array indexes start at 0 and end at size - 1. So you should really change your for loop to something like below else you're just wasting memory. It may only be a matter of bytes now, but you could possibly keep this "habit" and waste a significant amount of memory in larger projects.

From:
for (new idx = 1; idx<=totalpickups; idx++)

To:
for (new idx = 0; idx< totalpickups; idx++)
Heh

That actually reminds me of a minor flaw within my code, if you have a row missing an ID, (say for example you delete a pickup and there's a row ID missing, the array element is still there) make sure when you create new rows that you try to use IDs that were occupied with data but aren't any more.

A better example:
You create 60 pickup rows, you delete row ID 6, then row 6 is missing and the array element is still there.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)