Our Valgrind tests show the project has good memory management with:
- No "definitely lost" memory (true memory leaks)
- A small amount (5 bytes) of "still reachable" memory from a static variable
- Readline-related allocations that are permitted to remain "still reachable" per project requirements
-
Token Creation
- Each token is dynamically allocated
- Token values are duplicated strings (using
ft_strdup) - Tokens are linked together in a linked list
-
Token Cleanup
- The
free_tokensfunction properly traverses the list - Each token's value string is freed
- The token structure itself is freed
- The
-
String Extraction
- Fixed potential leak in
extract_double_quoted_string - Implemented proper cleanup in
extract_text_before_env_var - All temporary strings now correctly freed
- Fixed potential leak in
-
Command History
- Uses
last_executedstatic variable inhandle_input - This causes the 5-byte "still reachable" memory
- This is acceptable as it's reclaimed by OS at exit
- Uses
We're using Valgrind suppressions for:
- Readline library allocations
- Terminfo library memory
- Other system libraries used for terminal handling
This allows us to focus on our own memory management without noise from third-party libraries.
-
Ownership Rules
- Clear ownership transitions for allocated memory
- Functions that allocate memory are responsible for freeing it
- When ownership transfers, original owner no longer frees
-
Consistent Allocation Patterns
- Using matching allocation/deallocation functions
- Following "allocate early, free late" principle
- Ensuring error paths also free resources
-
Cleanup on Exit
- Free lexer tokens after processing
- Free command history strings when appropriate
- Properly terminate readline resources
-
Static Last Executed Variable
- 5 bytes "still reachable" in
shell_loop.c - Required for command history deduplication
- Not a true leak as program maintains reference until exit
- 5 bytes "still reachable" in
-
Readline Memory
- Per project spec, we don't need to fix readline memory leaks
- Suppressed in Valgrind output for clarity
-
Error Recovery
- All error paths should include proper cleanup
- When adding new features, ensure cleanup happens even in error cases
-
Regular Valgrind Checks
- Run with
./run-minishell.sh suppressed - Watch for any "definitely lost" memory
- Monitor "possibly lost" and "still reachable" blocks
- Run with
-
Edge Cases to Test
- Syntax errors in command input
- Incomplete quotes or parentheses
- Interrupted commands (Ctrl+C)
- Large input processing
The memory management is currently robust with no actual leaks. The small amount of "still reachable" memory is acceptable according to project requirements and standard C programming practices. When extending the codebase, maintain the current careful allocation/deallocation patterns.