Fix #9588, 140a96b: [Squirrel] Reaching memory limit during script registration could prevent further script detections #9589
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
When a script's
info.nut
triggersMaximum memory allocation exceeded
, it can prevent the detection of other scripts of the same type.This happens because the memory allocation triggering the limit is still accounted in the allocator for all other detections.
And that means this memory is also never freed.
Once the
allocated_size
issue was fixed, another issue appeared, a script failing to register due to memory limit would still be available ingame (and possibly other scripts that should have failed memory check). That's because scripts are scanned multiple times, and the error was not properly reset.Finally another memory leak exists, when the memory limit is triggered in a constructor and using placement new.
Regarding the reported crash, it's because
DummyAI
registration triggers the memory limit due to a previous failing script and incomplete cleaning between registrations.Description
Properly count allocated memory, and free memory not referenced by squirrel before it's lost.
I added an assert to ensure all allocated memory is managed by squirrel and its garbage collector, or is taken care of otherwise.
Limitations
As now the engine is properly reset, the failing scripts are now reported multiple times in debug console, because many rescan happen during startup.
Checklist for review
Some things are not automated, and forgotten often. This list is a reminder for the reviewers.