### Function

# GLib clear_fd

since: 2.76

#### Declaration [src]

    static inline gboolean
    g_clear_fd (
      int* fd_ptr,
      GError** error
    )

#### Description [src]

If `fd_ptr` points to a file descriptor, close it and return whether closing it was successful, like g_close(). If `fd_ptr` points to a negative number, return `TRUE` without closing anything. In both cases, set `fd_ptr` to `-1` before returning.

Like g_close(), if closing the file descriptor fails, the error is stored in both %errno and `error`. If this function succeeds, %errno is undefined.

On POSIX platforms, this function is async-signal safe if `error` is `NULL` and `fd_ptr` points to either a negative number or a valid open file descriptor. This makes it safe to call from a signal handler or a `GSpawnChildSetupFunc` under those conditions. See `signal(7)` and `signal-safety(7)` for more details.

It is a programming error for `fd_ptr` to point to a non-negative number that is not a valid file descriptor.

A typical use of this function is to clean up a file descriptor at the end of its scope, whether it has been set successfully or not:

    gboolean
    operate_on_fd (GError **error)
    {
      gboolean ret = FALSE;
      int fd = -1;

      fd = open_a_fd (error);

      if (fd < 0)
        goto out;

      if (!do_something (fd, error))
        goto out;

      if (!g_clear_fd (&fd, error))
        goto out;

      ret = TRUE;

    out:
      // OK to call even if fd was never opened or was already closed
      g_clear_fd (&fd, NULL);
      return ret;
    }

This function is also useful in conjunction with #g_autofd.

Available since: 2.76

This function is not directly available to language bindings.

#### Parameters

`fd_ptr`

  Type: `int*`

  A pointer to a file descriptor.

  The argument will be modified by the function.

`error`

  Type: `GError **`

  The return location for a recoverable error.

  The argument can be `NULL`.
  If the return location is not `NULL`, then you must initialize it to a `NULL` `GError*`.
  The argument will be left initialized to `NULL` by the function if there are no errors.
  In case of error, the argument will be set to a newly allocated `GError`; the caller will take ownership of the data, and be responsible for freeing it.

#### Return value

Type: `gboolean`

`TRUE` on success.