Jump to content

Crash after file write creates an empty file


EvilTurtleProductions
 Share

Recommended Posts

I noticed something interesting: if our game crashes, after saving the player stats to a file and having finished this, the save file is empty.

The bit of code below is the stats saving, wrapping up some gameplay-related things and then clearing the procedurally generated level.
Sometimes the game crashes when clearing the level, which is way after the stats file has been saved. Yet the file is empty.

Stream* statsfilestream = FileSystem::WriteFile("playerstats.sav");
if (statsfilestream)
{
    System::Print("Saving player stats: " + std::to_string(playerstats_local.XP) + ", " + std::to_string(playerstats_local.level) + ", " + std::to_string(playerstats_local.money));
    statsfilestream->WriteInt(playerstats_local.XP);
    statsfilestream->WriteInt(playerstats_local.level);
    statsfilestream->WriteInt(playerstats_local.money);
    statsfilestream->WriteInt(playerstats_local.logs);
    statsfilestream->Release();
}

//SOME EXTRA BITS AND BOBS OF CODE HERE
//SUCH AS UPDATING INGAME MISSION SCREENS AND PLAYING VOICE ACTING SOUNDS


//clear level
ClearLevel();

 

Link to comment
Share on other sites

The OS probably has a memory buffer that writes to the steam when a certain threshold size is reached, or when the file handle is closed. This would allow it to write a big block at once, instead of writing bytes in the same chunks you are writing them in.

  • Like 1

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...