Arqade is a question and answer site for passionate videogamers on all platforms. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am pretty new to PC gaming after having a PS3. I didn't make a switch to PS4 but instead went to PC gaming. So far, everything is awesome.

However there is a thing that worries me; while exiting a game using alt+F4 in Windows, could that cause the save game file to be corrupted, especially during the autosave?


Warning. These answers are general and may not apply to your specific game and under some uncommon circumstances hitting Alt-F4 could corrupt your save file anyway. (Example: game runs in Dosbox, or an emulator, or a terminal, or you press Alt-F4 on a window that's hung and then confirm that you want to kill the process.)

Use of ALT-F4 to exit a game is at your own risk.

share|improve this question
2  
I imagine it only effects the save data if you Alt+F4 while the game is saving your file. I can't imagine why else it would effect save data otherwise, it just doesn't make sense. – DJ Aftershock Jan 21 at 19:44
up vote 102 down vote accepted
+100

As a programmer, both answers posted so far are incorrect. While it's possible to come up with a hypothetical situation in which pressing Alt-F4 would corrupt a save in progress, actually doing so would require the developers to quite deliberately go out of their way to screw up the saving system.

From a coding perspective, the user pressing Alt-F4 does not "close the active window", nor does it "interrupt the program." What it does is cause Windows to place a WM_CLOSE message onto the program's event queue. That's all.

The event queue, as the name indicates, is a queue of events for the program to process; this mostly consists of input from the user. At the heart of the code for essentially every program driven by external input, including games, is what's known as an event loop, which checks for input on the event queue, processes it, then repeats the last two steps in a loop forever until it's time to shut down.

There are two things to keep in mind here. First is that the event loop is a linear thing: you don't process event #2 until you're finished processing event #1. And the second is that a WM_CLOSE message does not "quit the program". It's a special type of input, nothing more. It tells the program that the user has requested that the program close down the current window. The program is free to respond to this in whatever way its code says to, including ignoring it entirely. (This is a very rude thing to do, but developers occasionally do it.) One of the most common responses is to ask the user "Do you want to save before quitting?" and/or provide a way to cancel the close request.

So what happens if the user presses Alt-F4 while the game is in the middle of saving? Keep in mind the first point: processing is linear. Assuming that the save is taking place in the main thread, (which I'll cover a bit further on,) the code can't even check the event queue to see that it's been sent a WM_CLOSE message until after saving is complete. Therefore there's nothing to interrupt.

It's always possible to have a computer program doing two things at once. This is known as multithreading, running two or more linear "threads" of code execution at the same time. So someone might ask, "what if it's saving in a different thread while the WM_CLOSE message comes in and gets processed?" The answer to that question is that any developers who do this, their code is likely to corrupt savefiles left, right and center even without the user requesting a quit at an inopportune moment. This is because saving means writing out to disc a copy of the state of the game at the moment. If you do this without interrupting the game, it's possible that something can change in between when you start to save and when you finish, and then the save file ends up with some data that refers to the old state of the game and some that refers to the new state, which don't make any sense together, and you now have a corrupted save file.

This is actually one of the first things anyone learns about multithreading: never let two threads touch the same data at the same time if one (or both) of them is going to be changing it. Failing to follow this principle creates race conditions where data gets corrupted and things fail in bizarre ways. Any competent developer is going to go out of their way to avoid scenarios in which a race condition would pop up. So one developer checking in code that performs a save on a separate thread would most likely be looked upon with horror by the rest of the dev team!

TL;DR: If your game already performs autosaves without corrupting itself, it's safe to assume that there is no risk of corruption in politely asking the game to shut down, (which is what ALT-F4 does,) even if you do so in the middle of a save. When warning screens tell you not to shut down the game while saving, it refers to turning off the power or other more drastic ways of terminating gameplay.

share|improve this answer
1  
Comments are not for extended discussion; this conversation has been moved to chat. – StrixVaria Jan 16 at 0:44
3  
Stop discussion here. New discussion goes in chat. – StrixVaria Jan 16 at 11:55
10  
This answer has a Meta post. Please contribute your thoughts. – Frank Jan 17 at 20:03

Anything that interrupts the program while it is writing can corrupt the save file. If you want to avoid corrupting saves, use the game's built-in sequence to exit the program.

share|improve this answer
3  
Stop discussion here. If you have anything to discuss, please do so in chat. – fredley Jan 18 at 14:43

ALT+F4 is generally ok to shut down a game. The main issue is that doing so will tell the game that you would like it to shut down and it depends on how the programmer decided to handle when a user presses ALT+F4 when the game hasn't been saved.

This isn't really an issue for game that you save yourself by pausing and selecting "save" from the menu. But some games have "autosaving". They will automatically save your game as you're playing. If you ALT-F4 during one of those save processes you very well may corrupt your save data if the game was programmed badly.

If you add ask it to quit the game while it is saving it technically should finish saving before going through with your command to quit but there are (not often but it happens) times when a game has been programmed so badly it might use multithreading to save while also processing your quit command.

Usually games that have "autosave" will have an icon that appears in the corner of the screen somewhere to let you know it's saving. Most loading screens will tell you "Do not turn off or exit the game when you see this icon".

Here's a couple example of games telling you about their autosave icons: Autosave Loading Screen 1

Autosave Loading Screen 2

TL;DR: It's generally OK to use ALT+F4 to close a game. There are some unlikely but possible times it could possibly corrupt save data but it probably won't. If you want to be safe, use the in-game menu to quit the game.

share|improve this answer
    
Stop discussion here. If you have anything to discuss, please do so in chat. – fredley Jan 18 at 14:44

If the application crashes when it quits (which is a known issue in Undertale, Cities: Skylines, Dragon Age: Origins, and lots of other games), then you should not quit it in the middle of saving. @MasonWheeler's answer assumes the game does not crash. Crashing aborts everything, terminates all the threads, and is generally a very abrupt way of exiting a program.

While it's possible that all the writes have already been handed off to the kernel (and will be completed automatically), it's also possible that the game performs multiple writes (e.g. Minecraft actually uses multiple save files and must be using a multiple-write technique), in which case an interruption at the wrong time might leave only some of those writes complete. Furthermore, Linux (and possibly Mac OS X, I have not investigated) does not have real asynchronous writes (the aio_* functions are actually just synchronous writes running on top of a userspace thread pool), meaning it isn't really possible to "hand writes off to the kernel" in the first place on that platform.

Since you cannot know that your game doesn't have a crashing bug, you should not Alt+F4 at all unless you like to live dangerously.

share|improve this answer
1  
fair point but I'm disaggreeing to your conclusion. Your conlusion is applicable for everything so this would lead to: Don't use any feature of closed source software since you can't know it hasn't any bugs. And thats ridiciouless! – Zaibis Jan 18 at 12:09
1  
@Zaibis: There is little to no advantage to using Alt+F4. Why take unnecessary risks? – Kevin Jan 18 at 15:42
    
I'm not arguing about advantages of alt f4 shortcut. Im even with you here. but the arguments base about it is something I really dont see this way. – Zaibis Jan 18 at 16:09
3  
For the record, this isn't about open source vs closed source. Any software can have bugs. You have to judge the benefits and risks on a case by case basis. I judge Alt+F4 to be not worth it. That's all my answer is saying. – Kevin Jan 18 at 16:12
2  
@Zaibis: I have two whole paragraphs of the former and one sentence of the latter (which, by the way, someone edited in a way that I have since partially rolled back -- see edit history for details). – Kevin Jan 18 at 20:13

According to Microsoft's Guidelines for Keyboard user Inerface, Alt + F4 should "Close the active item, or quit the active application."

On application level, that should dispacth a WM_QUIT message. For applications with unsaved or dirty "documents" open, this again should present the user with a message asking whether or not to save the changes. Hence if the programmers cared to abide to all those specifications, Alt + F4 should ask you whether you want to save the game state. However, not everybody cares for specs, so it might be safe on some games, and not safe on some.

share|improve this answer
7  
"that should dispacth a WM_QUIT message", no, it queues a WM_CLOSE, which by default will close the window, but not exit the program. WM_CLOSE has to be handled by the program to call PostQuitMessage(), which, when handled properly, should exit the main loop of the program. – isanae Jan 15 at 4:30

However there is a thing that worries me; while exiting a game using alt+F4 in Windows, could that cause the save game file to be corrupted, especially during the autosave?

The short-answer

It depends on the game, and I have found that ALT+F4 is generally a safe way of closing down a game. Corruption is never intended so is better explained by bugs or poor code design.

The long-answer:

According to Microsoft's official documentation:

The user can close an application window by clicking the Close button, or by using a keyboard shortcut such as ALT+F4. Any of these actions causes the window to receive a WM_CLOSE message. The WM_CLOSE message gives you an opportunity to prompt the user before closing the window.

Therefore, using ALT+F4 is subject to how the game has been programmed to handle WM_CLOSE messages. However, there are other factors to be aware of:

  • What other processes are running (e.g. auto-save)
  • Code quality (i.e. potential for bugs)

From personal experience, I have never seen game corruption when using ALT+F4 mid-game. The only affects I have noticed is that you revert to your last saved checkpoint/save file (depends on the game). I actually use ALT+F4 often, preferring it to clicking around for 2 minutes trying to exit a game through menus.

You should find that any game that offers a save function will make a copy of the game world at a specific point. As long as there is an ability to load the save (normally through a Load option in the menu, but can also apply to automatically placing you at your last saved checkpoint in some games) then you should be confident that ALT+F4 mid-game is safe. In fact, you find games that offer to save when using ALT+F4.

However, ALT+F4 during the save process may carry a risk. The code would need to handle this scenario specifically. It is a case of basically allowing the auto-save functionality to finish running once a WM_CLOSE message is received.

I say basically, but it can be tricky getting the code right. Even more so if you factor in that the game is probably using threads. The threads critical to the saving of the game must be allowed to complete before closing the application. Different versions of Windows also have subtle differences in how they implement threads (as does the library that you use, even C++ Standard Library changes between versions).

When it comes to threading, so many things can go wrong. Buggy code and poor design are two factors. It is also possible that slightly different things may occur depending on when (during the process) you press ALT+F4. You may get different behaviour if you press it before ANY save file has been written.

A lot of games that offer auto-saving have a warning message telling you to not power off the machine during an auto-save. I would never consider ALT+F4 is a good idea mid-save. It may be safe, but there is no guarantee. Technically, the warning messages are talking about scenarios where a save file may be partially written because power was lost mid-process, but I like to be cautious. Losing hours that you put into a game is very frustrating. Why risk it?

One other point to consider is the destination of save "files". More and more games are now moving away from local files and onto Cloud based services and other web services. ALT+F4 during such a saving process could potentially partially write the data (and therefore create a corruption).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.