Ensure all error paths properly clean up resources and handle errors appropriately. This includes: 1. Using errdefer for cleanup when allocating resources
Ensure all error paths properly clean up resources and handle errors appropriately. This includes:
Example of proper resource cleanup:
// Bad - potential resource leak
const data = allocator.alloc(u8, size);
const result = processData(data); // May fail
if (result == .err) return error.ProcessFailed;
// Good - cleanup guaranteed on error
const data = try allocator.alloc(u8, size);
errdefer allocator.free(data);
const result = try processData(data);
// For multiple resources
const key = try allocator.dupe(u8, input_key);
errdefer allocator.free(key);
try map.put(allocator, key, value);
Common issues to watch for:
This practice helps prevent resource leaks and ensures robust error handling across all execution paths.
Enter the URL of a public GitHub repository