libvdo
GLib and error handling

The vdo library it built on top of GLib, which is a general-purpose, portable utility library for C. This library provides functionality, for implementing the VDO objects above, but also useful macros.

GError is used for error handling. The GError is a struct that contains an error code and an error message. Most vdo API functions have a double pointer to GError parameter, which is used to set a location to a GError pointer. If the double pointer is instead NULL, errors are ignored.

A commonly used feature is the g_autoptr, which allows for automatic freeing of a pointer to heap allocated memory. In the example below, this is used for both the GError and VdoStream object:

{
g_autoptr(GError) error = NULL;
g_autoptr(VdoStream) stream = vdo_stream_new(settings, NULL, &error);
if (!stream) {
printf("Error: %s", error->message);
return false;
}
// Use stream object here...
}
// Both error and stream are freed here, when they go out of scope.
A video stream.
VdoStream * vdo_stream_new(VdoMap *settings, VdoBufferFinalizer fin, GError **error)
Create a new VdoStream.

This is the recommended usage pattern of the VDO API.

NOTE: when using g_autoptr, the pointer must not be freed manually. It should also not be reassigned to something else. An exception to the latter case, is reusing a non-null GError in a function that continues after a failed vdo call. In that case, the error first need to be cleared with g_clear_error:

{
g_autoptr(GError) error = NULL;
g_autoptr(VdoStream) stream = vdo_stream_new(settings, NULL, &error);
if (!stream) {
g_clear_error(&error); // Reset error before reuse.
}
// Reuse the error with some other vdo function.
g_autoptr(VdoChannel) channel = vdo_channel_get(1, &error);
}
A video channel.
VdoChannel * vdo_channel_get(guint channel_nbr, GError **error)
Get an existing channel.

If g_autoptr is not used, GErrors should be freed using g_clear_error, and each VDO object needs to be freed using g_clear_object (these functions which will also set the pointer to NULL). Example:

GError* error = NULL;
VdoStream* stream = vdo_stream_new(settings, NULL, &error);
// Use stream object here...
g_clear_error(&error);
g_clear_object(&stream);

As previously stated, this is not recommended. Use g_autoptr when possible instead.