Function Macro

GLibSAVE_ERRNO

unstable since: 2.92

Declaration [src]

#define G_SAVE_ERRNO (
  var
)

Description [src]

Helper to save and restore errno.

This macro declares a variable named var, and initializes it from errno. When control flow leaves the enclosing scope, the saved value is stored back into errno. This is convenient to use when handling an error using functions that could overwrite (“clobber”) the value of errno, such as anything that might allocate memory:

if (g_unlink (path) < 0)
  {
    G_SAVE_ERRNO (errsv);
    g_autofree char *details = get_more_details ();

    g_info ("could not delete %s (%s): %s",
            path, details, g_strerror (errsv));
  }

// now errno is as set by g_unlink(), even if get_more_details()
// or g_info() temporarily changed it

This feature is only supported on GCC and clang. This macro is not defined on other compilers and should not be used in programs that are intended to be portable to those compilers.

Available since: 2.92

This function is not directly available to language bindings.

Parameters

var

Type: -

A variable name, traditionally errsv.