calc-moon: 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 20:23:38 +02:00
rodzic a9493dc507
commit 11b4f96592
1 zmienionych plików z 9 dodań i 16 usunięć

Wyświetl plik

@ -68,8 +68,8 @@ picplanner_get_illumination (GDateTime *date_time)
{
double illumination;
double elongation;
double *coordinates_moon;
double *coordinates_sun;
g_autofree double *coordinates_moon = NULL;
g_autofree double *coordinates_sun = NULL;
double ra_sun, dec_sun, ra_moon, dec_moon;
coordinates_moon = picplanner_get_coordinates_rotational_moon (date_time);
coordinates_sun = picplanner_get_coordinates_rotational_sun (date_time);
@ -82,9 +82,6 @@ picplanner_get_illumination (GDateTime *date_time)
elongation = sin(dec_sun)*sin(dec_moon) + cos(dec_sun)*cos(dec_moon)*cos(ra_sun-ra_moon);
illumination = (1 - elongation)/2;
g_free (coordinates_moon);
g_free (coordinates_sun);
return illumination*100.;
}
@ -95,7 +92,7 @@ double
double latitude)
{
double siderial_time;
double *coordinates_moon;
g_autofree double *coordinates_moon = NULL;
double *coordinates_horizontal_moon;
coordinates_moon = picplanner_get_coordinates_rotational_moon (date_time);
@ -104,7 +101,6 @@ double
latitude,
siderial_time);
g_free (coordinates_moon);
return coordinates_horizontal_moon;
}
@ -115,22 +111,19 @@ double
double longitude,
double latitude)
{
GDateTime *iteration_time;
double *coordinates_moon;
double *array_coordinates_moon = malloc (sizeof (double) * 2 * NUM_DATA_POINTS);
for (int i=0; i<NUM_DATA_POINTS; i++)
{
iteration_time = g_date_time_add_minutes (date_time, i*24*60/NUM_DATA_POINTS-12*60);
g_autoptr (GDateTime) iteration_time =
g_date_time_add_minutes (date_time, i*24*60/NUM_DATA_POINTS-12*60);
g_autofree double *coordinates_moon =
picplanner_get_coordinates_moon (iteration_time,
longitude,
latitude);
coordinates_moon = picplanner_get_coordinates_moon (iteration_time,
longitude,
latitude);
array_coordinates_moon[2*i] = coordinates_moon[0];
array_coordinates_moon[2*i+1] = coordinates_moon[1];
g_free (coordinates_moon);
g_date_time_unref (iteration_time);
}
return array_coordinates_moon;