Linux - Checking assumptions in the code using commands from the glib library

Repository

https://github.com/Vitusc

What Will I Learn?

You will learn how to check the assumptions in the code using the available commands in the glib library.
You will also learn how to verify the conditions correctly.

Requirements

  • Linux OS
  • Good knowledge of linux
  • Basics of programming in this system
  • Good understanding and reading of the code
  • Knowledge of the use of linux libraries.
  • Reading comprehension

Difficulty

  • Intermediate / Advanced

Tutorial Contents

The glib library provides a set of definitions that are often used to check
assumptions in the code. Thanks to them errors in programs are most often detected faster. It should be
place these definitions in sensitive areas of the code to check the necessary conditions. If a condition fails to be verified, the definitions print a warning in the console. It is possible that they will force a return to the calling function, and ultimately
ending the application.
Definitions are divided into two types

  1. commonly used to validate the arguments provided by the calling function
  2. verifying the conditions within the function.

Considering the correctness of arguments is the first step when we start the function process. They are so-called checking the necessary conditions. Two definitions:


g_return_val_if_fail(condition,returned_value) and g_return_if_fail(condition)

they print a warning, if (condition!=TRUE)
and they come back from the function. While the first of these
function returns

returned_value

it must be used for functions that are not declared in an empty context (void), the other is used in functions that do not pass values ​​(void functions).

in GNOME you can find an example of panel implementation - here is a clipping:

void
panel_clean_applet(AppletInfo *info)
{
g_return_if_fail(info != NULL);

if(info->widget) {
if(info -> type == APPLET_STATUS) {
status_applet_put_offscreen(info-data);
}
gtk_widget_destroy(info->widget);
}
}

You can try these definitions and let you know if you are leaving.
In case of any problems, you can speak in a comment.
Regards, @vitusc!

H2
H3
H4
3 columns
2 columns
1 column
Join the conversation now