exit_error - 1Fr3aK2/Cub3d GitHub Wiki

📝 exit_error

Terminates the program with an error message and performs cleanup of allocated resources.

⚙️ Parameters

Parameter Type Description
data t_data * Pointer to the main data structure. If not NULL, it will be freed before exiting.
str char * Error message to display. If NULL, a generic "Error" message is printed.

🔁 Returns

Return value Description
void The function does not return. The program terminates with exit code 1.

📖 Description

The exit_error function is used to handle fatal errors in the program. It performs the following steps:

  1. Prints "Error\n" if str is NULL; otherwise prints the content of str to standard error.
  2. If data is not NULL, calls free_all(data) to release all the allocated memory and sets data to NULL.
  3. Closes all open file descriptors by calling close_fds(0).
  4. Terminates the program with exit(1).

This function ensures that all allocated resources are freed and that the program exits cleanly in case of errors.

💡 Example Usage

t_data data;

// Some code that detects a fatal error
if (!map_valid)
{
    exit_error(&data, "Invalid map file.");
}