sun-view: Prefer g_auto* macros

g_autofree will automatically free variables when they go out of
scope. g_autoptr will do the same, but using the appropriate freefunc
for the given type, e.g. GDateTime -> g_date_time_unref() or
GObject -> g_object_unref().

This automatic cleanup reduces the risk of memory leaks
and allows getting rid of some lines of code.
main
Evangelos Ribeiro Tzaras 2023-07-17 19:46:50 +02:00
rodzic 9665a95248
commit 359bbee260
1 zmienionych plików z 28 dodań i 68 usunięć

Wyświetl plik

@ -61,24 +61,24 @@ picplanner_sun_set_rise_upper_set (PicplannerSun *sun,
double *coordinates_array,
int *index_rise_upper_set_lower)
{
char *char_morning_rise_time;
char *char_morning_rise_azimuth;
g_autofree char *char_morning_rise_time = NULL;
g_autofree char *char_morning_rise_azimuth = NULL;
char *char_upper_time;
char *char_upper_azimuth;
char *char_upper_elevation;
g_autofree char *char_upper_time = NULL;
g_autofree char *char_upper_azimuth = NULL;
g_autofree char *char_upper_elevation = NULL;
char *char_lower_time;
char *char_lower_azimuth;
char *char_lower_elevation;
g_autofree char *char_lower_time = NULL;
g_autofree char *char_lower_azimuth = NULL;
g_autofree char *char_lower_elevation = NULL;
char *char_evening_set_time;
char *char_evening_set_azimuth;
g_autofree char *char_evening_set_time = NULL;
g_autofree char *char_evening_set_azimuth = NULL;
GDateTime *date_time_rise;
GDateTime *date_time_upper;
GDateTime *date_time_set;
GDateTime *date_time_lower;
g_autoptr (GDateTime) date_time_rise = NULL;
g_autoptr (GDateTime) date_time_upper = NULL;
g_autoptr (GDateTime) date_time_set = NULL;
g_autoptr (GDateTime) date_time_lower = NULL;
date_time_rise = g_date_time_add_minutes (date_time,
@ -159,26 +159,6 @@ picplanner_sun_set_rise_upper_set (PicplannerSun *sun,
gtk_label_set_text (GTK_LABEL (sun->label_lower_time), char_lower_time);
gtk_label_set_text (GTK_LABEL (sun->label_lower_azimuth), char_lower_azimuth);
gtk_label_set_text (GTK_LABEL (sun->label_lower_elevation), char_lower_elevation);
/*
* Free allocated memory.
*/
g_date_time_unref (date_time_rise);
g_date_time_unref (date_time_upper);
g_date_time_unref (date_time_set);
g_free (char_morning_rise_time);
g_free (char_morning_rise_azimuth);
g_free (char_upper_time);
g_free (char_upper_azimuth);
g_free (char_upper_elevation);
g_free (char_lower_time);
g_free (char_lower_azimuth);
g_free (char_lower_elevation);
g_free (char_evening_set_time);
g_free (char_evening_set_azimuth);
}
@ -191,21 +171,21 @@ picplanner_sun_set_dark_blue_golden (PicplannerSun *sun,
GDateTime *date_time,
int *index_dark_blue_golden)
{
char *char_dark_night_morning;
char *char_blue_hour_morning;
char *char_golden_hour_morning;
GDateTime *dark_night_morning_end;
GDateTime *blue_hour_morning_begin;
GDateTime *blue_hour_golden_hour_morning_switch;
GDateTime *golden_hour_morning_end;
g_autofree char *char_dark_night_morning = NULL;
g_autofree char *char_blue_hour_morning = NULL;
g_autofree char *char_golden_hour_morning = NULL;
g_autoptr (GDateTime) dark_night_morning_end = NULL;
g_autoptr (GDateTime) blue_hour_morning_begin = NULL;
g_autoptr (GDateTime) blue_hour_golden_hour_morning_switch = NULL;
g_autoptr (GDateTime) golden_hour_morning_end = NULL;
char *char_golden_hour_evening;
char *char_blue_hour_evening;
char *char_dark_night_evening;
GDateTime *golden_hour_evening_begin;
GDateTime *golden_hour_blue_hour_evening_switch;
GDateTime *blue_hour_evening_end;
GDateTime *dark_night_evening_begin;
g_autofree char *char_golden_hour_evening = NULL;
g_autofree char *char_blue_hour_evening = NULL;
g_autofree char *char_dark_night_evening = NULL;
g_autoptr (GDateTime) golden_hour_evening_begin = NULL;
g_autoptr (GDateTime) golden_hour_blue_hour_evening_switch = NULL;
g_autoptr (GDateTime) blue_hour_evening_end = NULL;
g_autoptr (GDateTime) dark_night_evening_begin = NULL;
dark_night_morning_end = g_date_time_add_minutes (date_time,
index_dark_blue_golden[0]*24*60/NUM_DATA_POINTS-12*60);
@ -354,26 +334,6 @@ picplanner_sun_set_dark_blue_golden (PicplannerSun *sun,
gtk_label_set_text (GTK_LABEL (sun->label_evening_golden), char_golden_hour_evening);
gtk_label_set_text (GTK_LABEL (sun->label_evening_blue), char_blue_hour_evening);
gtk_label_set_text (GTK_LABEL (sun->label_evening_dark), char_dark_night_evening);
/*
* Free allocated memory.
*/
g_date_time_unref (dark_night_morning_end);
g_date_time_unref (blue_hour_morning_begin);
g_date_time_unref (blue_hour_golden_hour_morning_switch);
g_date_time_unref (golden_hour_morning_end);
g_date_time_unref (golden_hour_evening_begin);
g_date_time_unref (golden_hour_blue_hour_evening_switch);
g_date_time_unref (blue_hour_evening_end);
g_date_time_unref (dark_night_evening_begin);
g_free (char_dark_night_morning);
g_free (char_blue_hour_morning);
g_free (char_golden_hour_morning);
g_free (char_golden_hour_evening);
g_free (char_blue_hour_evening);
g_free (char_dark_night_evening);
}
static void