Compilation Flags
The Makefile includes debugging symbols by default:Makefile:19
-g
Generates standard debugging information for GDB
-g3
Includes extra debugging information like macro definitions
-O3
Full optimization - may complicate debugging but matches release behavior
-Werror
Treats all warnings as errors - ensures code quality
The combination of
-O3 with -g -g3 allows debugging optimized code while maintaining performance. If debugging is difficult due to optimization, temporarily change -O3 to -O0 in the Makefile.Using GDB
Basic GDB Workflow
Useful GDB Commands
Debugging Raycasting Issues
Common raycasting bugs and how to debug them:Walls appear distorted or stretched
Walls appear distorted or stretched
Likely cause: Incorrect wall distance calculationCheck that
dist is positive and reasonable. The wall height calculation in raycasting.c:76:raycasting.c:68-82
Wrong textures on walls
Wrong textures on walls
Likely cause: Texture selection logic errorVerify texture selection in
raycasting.c:84-100:raycasting.c:84-100
Player clips through walls
Player clips through walls
Likely cause: Collision detection issue in Check the collision detection logic in
is_wall()utils6.c:15-27.Memory Debugging with Valgrind
Basic Valgrind Usage
--leak-check=full- Show detailed leak information--show-leak-kinds=all- Show all types of leaks--track-origins=yes- Track origin of uninitialized values
Suppressing MLX Leaks
Create a suppression filemlx.supp:
mlx.supp
Common Memory Issues
- Memory Leaks
- Invalid Reads/Writes
- Uninitialized Values
Check these locations:Verify all exit paths call cleanup:Ensure
- Map parsing - ensure
freeme()is called on allocated arrays - Texture paths - verify
freetextures()infreemem.c:39-49 - Map duplication - check
final_mapandtoflood_mapcleanup
freemem.c:51-56
freeall() is called before each exit().Visual Debugging
Drawing Debug Information
Add debug visualization to understand what’s happening:Debug Helper Example
Print Debugging
Remove all debug prints before final submission as they violate the norm and slow rendering.
Debug Print Examples
Performance Profiling
Identify slow functions:- Functions called most frequently
- Functions taking most cumulative time
- Unexpected call patterns
Common Issues and Solutions
Segmentation Fault on Startup
Segmentation Fault on Startup
Probable causes:Check that
- Invalid map file path
- Corrupted .cub file
- NULL pointer in
t_mapstructure
init_values() is called before any structure access.Black Screen / No Rendering
Black Screen / No Rendering
Possible issues:Verify texture paths in .cub file are correct and files exist.
- Textures failed to load
- Rendering loop not running
- Image not being put to window
Crash During Movement
Crash During Movement
Likely causes:Check collision detection in
- Map array out of bounds access
- Invalid player position
is_wall() and movement code in move_cubed().Error: 'Error cargando textura'
Error: 'Error cargando textura'
Meaning: MLX failed to load a texture fileSolutions:
- Verify texture file exists:
ls -la path/to/texture.xpm - Check file permissions:
chmod 644 *.xpm - Validate XPM format - must be valid XPM
- Check path in .cub file matches actual file location
main.c:29-41:main.c:29-41
Map Parsing Errors
Map Parsing Errors
Error messages defined in cub3.h:
Debug parsing:
| Error | Cause |
|---|---|
ARGERROR | Wrong number of arguments |
ARGERROR2 | Empty argument |
ARGERROR3 | File doesn’t end with .cub |
MAPERROR3 | Empty map file |
MAPERROR4 | Map not closed (walls) |
MAPERROR10 | Attribute not found |
MAP_ARG_DUP | Duplicate attribute |
SPOTERROR | Multiple players or invalid characters |
RGBERROR | RGB format error |
Error Message Interpretation
All error messages are defined as macros incub3.h:42-66:
cub3.h:42-66
grep to find where each error is triggered: