-
Notifications
You must be signed in to change notification settings - Fork 40
Fix Below the Surface - Game fails to compile or run #247
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Fix Below the Surface - Game fails to compile or run #247
Conversation
The center_point() function was being called on sprites, rather than circles as intended. These calls have been updated to sprite_center_point() instead.
The game was crashing after the title screen and when the player moved, due to the order of state transitions. When change_state() was called mid-update loop, control flow continued on the now-deleted state, causing segmentation faults. Queued state transitions fix the problem by delaying state changes until after the current update loop. Rather than calling change_state() directly, affected classes now call request_state_change() to queue the next state for later. This approach has been used everywhere states were utilised: - Screens - Player - Enemies
RealH4D35
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixes definitely work
- Game now compiles
- Game runs without segmentation fault
- State transition logic is properly implemented
Note for future work: There are still 109 warnings when trying to compile. Consider addressing these in a future cleanup PR to improve code quality.
Some of the warnings when trying to compile:
Tested on Arch Linux using Clang++
Broccoli811
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The project compiles successfully, indicating correct syntax and linkage, however during the gameplay runtime a segmentation fault occurs. This suggests unsafe memory access. The most likely causes include invalid resource usage (bitmaps, sounds, or animations not properly loaded), out of bounds access to dynamic containers (such as enemies), or the use of freed or null objects during update or draw cycles.
Introducing defensive checks around resource loading, validating container indices, and avoiding mutation of collections during iteration would significantly improve runtime stability.
I ran the game again through gdb to print the backtrace. It appears its giving the bad memory patterns "0xbaadf00d" which means 'new_state' used to exist, but it doesnt anymore. You may need to revise some code to add guards 'Defensive coding' so this never silently crashes.
New states for AI (*machine.h) are now initialised as nullptr, meaning the null check in apply_next_state works as expected, preventing the segmentation fault. Player.h has been updated for consistency with the other class constructors.
|
Thanks @Broccoli811, I appreciate the detailed report. After playing further in the game I ran into the same issue. The problem was FlyMachine(FlyMachineState *state, sprite enemy_sprite) : state(nullptr), next_state(nullptr)All AI classes have been updated to match, and the Player class has been updated to use this syntax rather than being initialised at field declaration. I've re-tested on WSL and MSYS2 with no errors. |
Description
Problems
Below the Surface currently fails to compile using the standard command:
skm g++ program.cpp -o game. Error output here.Once the compilation error is amended, the game crashes after the first splash screen with a segmentation fault.
Causes
Compilation errors
The
center_point()function is being passed the wrong data type (sprite instead of circle), which causes compilation to fail.Runtime error
When transitioning between scenes, player states, etc., the state change occurs mid-update. Which means the state is changed (with a direct call to
change_state()), then the control flow of the update is passed back to the old (now deleted) state, causing the segmentation fault. State transitions apply to players, screens, and enemies.Fixes
center_point()calls withsprite_center_point()Now any class which uses states also includes
next_stateandnext_state_typefields. Rather than callingchange_state()mid-update,request_state_change()is called, which queues up the next state (and type) using the new fields. Once the update is finished,apply_next_state()is called, to change the state.So the control flow has changed from:
Start
update()->change_state()-> finishupdate()to:
Start
update()->request_state_change()-> finishupdate()->apply_next_state()Type of change
How Has This Been Tested?
On WSL:
Compiled with
skm g++ program.cpp -o gameRan with
./gameCompilation succeeded:

Game runs:

Checklist