Server crashing on fread
#1

pawn Код:
public OnGameModeInit()
{
    SetGameModeText("- City DM v1.0 -");
   
    new string[128],File:spawnfile = fopen("CityDM/teams.ini",io_read);
    while(fread(spawnfile,string))
    {
        sscanf(string,"p<,>s[15]dddds[30]",TeamInfo[TeamsCreated][TeamName],TeamInfo[TeamsCreated][TeamSkin],TeamInfo[TeamsCreated][TeamWeapon][0],TeamInfo[TeamsCreated][TeamWeapon][1],TeamInfo[TeamsCreated][TeamWeapon][2],TeamInfo[TeamsCreated][spawnfilename]);
        TeamInfo[TeamsCreated][TeamCreated] = 1;
        AddPlayerClass(TeamInfo[TeamsCreated][TeamSkin],0.0,0.0,0.0,0.0,TeamInfo[TeamsCreated][TeamWeapon][0],15000,TeamInfo[TeamsCreated][TeamWeapon][1],10000,TeamInfo[TeamsCreated][TeamWeapon][2],10000);
        printf("CREATED A TEAM (Team %s) (%d)",TeamInfo[TeamsCreated][TeamName],TeamsCreated);
        TeamsCreated ++;
    }
    fclose(spawnfile);
    for(new i = 0; i < TeamsCreated; i ++)
    {
        new count = SpawnCount[i],file[30];
        format(file,sizeof(file),"%s",TeamInfo[i][spawnfilename]);
        print(file);
        spawnfile = fopen(file,io_read);
        while(fread(spawnfile,string))
        {
                sscanf(string,"p<,>ffff",Spawns[i][count][X],Spawns[i][count][Y],Spawns[i][count][Z],Spawns[i][count][A]);
                printf("SPAWN ADDED (team %s) (%d) %.2f %.2f %.2f",TeamInfo[i][TeamName],i,Spawns[i][count][X],Spawns[i][count][Y],Spawns[i][count][Z]);
                count ++;
        }
        fclose(spawnfile);
        SpawnCount[i] = count;
    }
    return 1;
}
Is there any reason my server is crashing when it's trying to load the spawns when there is more than 1 spawn to load? In other words, when there is more than one (VALID) line in the file?
Reply
#2

You should always check whether the file handle is valid before using fread, fwrite, fclose.
pawn Код:
public OnGameModeInit()
{
    SetGameModeText("- City DM v1.0 -");
   
    new string[128],File:spawnfile = fopen("CityDM/teams.ini",io_read);
    if (spawnfile)
    {
        while(fread(spawnfile,string))
        {
            sscanf(string,"p<,>s[15]dddds[30]",TeamInfo[TeamsCreated][TeamName],TeamInfo[TeamsCreated][TeamSkin],TeamInfo[TeamsCreated][TeamWeapon][0],TeamInfo[TeamsCreated][TeamWeapon][1],TeamInfo[TeamsCreated][TeamWeapon][2],TeamInfo[TeamsCreated][spawnfilename]);
            TeamInfo[TeamsCreated][TeamCreated] = 1;
            AddPlayerClass(TeamInfo[TeamsCreated][TeamSkin],0.0,0.0,0.0,0.0,TeamInfo[TeamsCreated][TeamWeapon][0],15000,TeamInfo[TeamsCreated][TeamWeapon][1],10000,TeamInfo[TeamsCreated][TeamWeapon][2],10000);
            printf("CREATED A TEAM (Team %s) (%d)",TeamInfo[TeamsCreated][TeamName],TeamsCreated);
            TeamsCreated ++;
        }
        fclose(spawnfile);
    }
    for(new i = 0; i < TeamsCreated; i ++)
    {
        new count = SpawnCount[i],file[30];
        format(file,sizeof(file),"%s",TeamInfo[i][spawnfilename]);
        print(file);
        spawnfile = fopen(file,io_read);
        if (spawnfile)
        {
            while(fread(spawnfile,string))
            {
                sscanf(string,"p<,>ffff",Spawns[i][count][X],Spawns[i][count][Y],Spawns[i][count][Z],Spawns[i][count][A]);
                printf("SPAWN ADDED (team %s) (%d) %.2f %.2f %.2f",TeamInfo[i][TeamName],i,Spawns[i][count][X],Spawns[i][count][Y],Spawns[i][count][Z]);
                count ++;
            }
            fclose(spawnfile);
        }
        SpawnCount[i] = count;
    }
    return 1;
}
Another thing is that if you use io_read and the file doesn't exist, the server will crash but you said it exists so I skip it.
Reply
#3

It can't be different than valid since these files HAVE TO exist for the server to load. I have no idea how it could be different?

Edit: It says it's invalid, but I have no clue why it is (the second file for the spawns, but only invalid for i = 0
Reply
#4

The file handle is not if a file exists. File handle is whether a file opened successfully.

If for any reason, it won't open the file successful and it uses invalid handle in fread, fwrite, fclose functions, those will crash the server.

EDIT: Make sure that scriptfiles folder is both readable/writeable (the permissions).
Reply
#5

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
EDIT: Make sure that scriptfiles folder is both readable/writeable (the permissions).
Well, it works IF there isn't another file (in this case I have two files). So it must be read-and writeable.

In other words, it opens the file belonging to i = 0 if the file in i = 1 isn't filled.
Reply
#6

The only thing I can think of is if TeamInfo[index][spawnfilename] haven't stored a file that exists in some index and then the second way comes true (io_read in file that doesn't exist).

Do some debugging:
pawn Код:
public OnGameModeInit()
{
    SetGameModeText("- City DM v1.0 -");
   
    new string[128], File:spawnfile;
   
    if (!fexist("CityDM/teams.ini")) print("CityDM/teams.ini doesn't exist");
    else
    {
        spawnfile = fopen("CityDM/teams.ini",io_read);
        if (spawnfile)
        {
            while(fread(spawnfile,string))
            {
                sscanf(string,"p<,>s[15]dddds[30]",TeamInfo[TeamsCreated][TeamName],TeamInfo[TeamsCreated][TeamSkin],TeamInfo[TeamsCreated][TeamWeapon][0],TeamInfo[TeamsCreated][TeamWeapon][1],TeamInfo[TeamsCreated][TeamWeapon][2],TeamInfo[TeamsCreated][spawnfilename]);
                TeamInfo[TeamsCreated][TeamCreated] = 1;
                AddPlayerClass(TeamInfo[TeamsCreated][TeamSkin],0.0,0.0,0.0,0.0,TeamInfo[TeamsCreated][TeamWeapon][0],15000,TeamInfo[TeamsCreated][TeamWeapon][1],10000,TeamInfo[TeamsCreated][TeamWeapon][2],10000);
                printf("CREATED A TEAM (Team %s) (%d)",TeamInfo[TeamsCreated][TeamName],TeamsCreated);
                TeamsCreated ++;
            }
            fclose(spawnfile);
        }
    }
    for(new i = 0; i < TeamsCreated; i ++)
    {
        new count = SpawnCount[i],file[30];
        if (!fexist(TeamInfo[i][spawnfilename])) printf("%s doesn't exist", TeamInfo[i][spawnfilename]);
        else
        {
            spawnfile = fopen(TeamInfo[i][spawnfilename],io_read);
            if (spawnfile)
            {
                while(fread(spawnfile,string))
                {
                    sscanf(string,"p<,>ffff",Spawns[i][count][X],Spawns[i][count][Y],Spawns[i][count][Z],Spawns[i][count][A]);
                    printf("SPAWN ADDED (team %s) (%d) %.2f %.2f %.2f",TeamInfo[i][TeamName],i,Spawns[i][count][X],Spawns[i][count][Y],Spawns[i][count][Z]);
                    count ++;
                }
                fclose(spawnfile);
                SpawnCount[i] = count;
            }
        }
    }
    return 1;
}
Reply
#7

That's the weird part. The file name is stored properly in that string, but fopen doesn't open it with any file handle. I debugged it earlier today and everything is fine.
Reply
#8

Please run this one (I forgot about the invalid file handle before):
pawn Код:
public OnGameModeInit()
{
    SetGameModeText("- City DM v1.0 -");
   
    new string[128], File:spawnfile;
   
    if (!fexist("CityDM/teams.ini")) print("CityDM/teams.ini doesn't exist");
    else
    {
        spawnfile = fopen("CityDM/teams.ini",io_read);
        if (spawnfile)
        {
            while(fread(spawnfile,string))
            {
                sscanf(string,"p<,>s[15]dddds[30]",TeamInfo[TeamsCreated][TeamName],TeamInfo[TeamsCreated][TeamSkin],TeamInfo[TeamsCreated][TeamWeapon][0],TeamInfo[TeamsCreated][TeamWeapon][1],TeamInfo[TeamsCreated][TeamWeapon][2],TeamInfo[TeamsCreated][spawnfilename]);
                TeamInfo[TeamsCreated][TeamCreated] = 1;
                AddPlayerClass(TeamInfo[TeamsCreated][TeamSkin],0.0,0.0,0.0,0.0,TeamInfo[TeamsCreated][TeamWeapon][0],15000,TeamInfo[TeamsCreated][TeamWeapon][1],10000,TeamInfo[TeamsCreated][TeamWeapon][2],10000);
                //printf("CREATED A TEAM (Team %s) (%d)",TeamInfo[TeamsCreated][TeamName],TeamsCreated);
                TeamsCreated ++;
            }
            fclose(spawnfile);
        }
        else print("invalid file handle -> CityDM/teams.ini");
    }
    for(new i = 0; i < TeamsCreated; i ++)
    {
        new count = SpawnCount[i],file[30];
        if (!fexist(TeamInfo[i][spawnfilename])) printf("%s doesn't exist", TeamInfo[i][spawnfilename]);
        else
        {
            spawnfile = fopen(TeamInfo[i][spawnfilename],io_read);
            if (spawnfile)
            {
                while(fread(spawnfile,string))
                {
                    sscanf(string,"p<,>ffff",Spawns[i][count][X],Spawns[i][count][Y],Spawns[i][count][Z],Spawns[i][count][A]);
                    //printf("SPAWN ADDED (team %s) (%d) %.2f %.2f %.2f",TeamInfo[i][TeamName],i,Spawns[i][count][X],Spawns[i][count][Y],Spawns[i][count][Z]);
                    count ++;
                }
                fclose(spawnfile);
                SpawnCount[i] = count;
            }
            else printf("invalid file handle -> %s", TeamInfo[i][spawnfilename]);
        }
    }
    return 1;
}
And post what it prints. Those I mentioned in the first post are the only ways that fread can crash the server (prevented the first, the second left).
Reply
#9

I found out what causes it. It's caused by \r\n that starts a new line. Is there any way to fix this? I tried to use strfind and delete \r\n, but without succes.

I remember I debugged it like you did, and it printed the file name + an empty line under it. So that's why I'm quite sure it's caused by \r\n. (actually 100%)
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)