Fix #6322: Attempt to crash the script instead of the whole game #9047
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation / Problem
See #6322. A script allocating (way) too much memory can crash the game which is arguably worse than crashing just the script.
Description
The solution is two fold:
As side effect some extra information about the allocation was added to the error message that could potentially help the script's developer, but that required changing the error message in Script_FatalError from char* to std::string&.
Limitations
Scripts can now be killed earlier, and probably even during saving, when the memory limit is reached. Previously if the memory was returned before the save or tick finished, the script would just continue.
It is a somewhat open question whether the old behavior of limit testing should be maintained, so the AI can for short bursts and saving use more memory. That would mean this change does not impact the scripts as much, however the benefit of doing it when the allocation happens is that the log will show exactly where the limit got exceeded and by how much. This could then help the script developer see where a huge amount of memory got allocated (in error) and that would make fixing the underlying issues in the scripts easier.
In case the OS is out of memory, and there is not even enough memory to go through the script engine cleanup the game will still crash out with MallocError. Though, not going through the script engine cleanup will leave some "backups" to be not restored so the game would be in an undefined state and it would likely crash at another place.
Checklist for review
Some things are not automated, and forgotten often. This list is a reminder for the reviewers.