From a948ca19fcf038374041abd906b2a0b0cd56b165 Mon Sep 17 00:00:00 2001 From: htom Date: Thu, 8 Jan 2026 21:16:11 +0100 Subject: [PATCH] new memory free strategy that relies on goto --- src/main.c | 46 ++++++++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/src/main.c b/src/main.c index e82be1f..c410c4b 100644 --- a/src/main.c +++ b/src/main.c @@ -68,7 +68,6 @@ void get_data_from_db(const char *query, duckdb_connection *conn, TelemetryInfo info->data_last_index++; break; } - // printf("%f, ", ((float *)col1_data)[row]); } else { printf("NULL"); } @@ -80,44 +79,44 @@ void get_data_from_db(const char *query, duckdb_connection *conn, TelemetryInfo } int main(int argc, char **argv) { + duckdb_database db; + duckdb_connection conn; + duckdb_result lap_count_res; + LapInfo *session_data = NULL; + double *lap_timestamps = NULL; + printf("\nHello from HardCompound!\n"); if (argc < 2) { printf("You need to specify a duckdb file path! Terminating!\n"); - exit(ERR_ARGUMENT); + goto cleanup; } if (!is_valid_path(argv[1])) { printf("The file you gave is not valid. Exiting!"); - exit(ERR_FILE); + goto cleanup; } printf("First argument: %s\n", argv[1]); - duckdb_database db; if (duckdb_open(argv[1], &db) == DuckDBError) { printf("Error opening duckdb file, terminating!\n"); - exit(ERR_DATABASE); + goto cleanup; } - duckdb_connection conn; if (duckdb_connect(db, &conn) == DuckDBError) { printf("Error connecting to the duckdb database! Terminating!\n"); - duckdb_close(&db); - exit(ERR_CONNECTION); + goto cleanup; } // get how many laps is in the session and allocate that many LapInfo-s - duckdb_result lap_count_res; int32_t lap_count = 0; duckdb_query(conn, "Select count(value) from Lap\n", &lap_count_res); duckdb_data_chunk data_chunk = duckdb_fetch_chunk(lap_count_res); if (!data_chunk) { printf("Error in retrieving laps"); - duckdb_disconnect(&conn); - duckdb_close(&db); - exit(ERR_QUERY); + goto cleanup; } // set column @@ -136,22 +135,22 @@ int main(int argc, char **argv) { duckdb_destroy_result(&lap_count_res); printf("Lap count: %d\n", lap_count); - LapInfo *session_data = calloc(lap_count, sizeof(LapInfo)); - double *lap_timestamps = calloc(lap_count, sizeof(double)); + session_data = calloc(lap_count, sizeof(LapInfo)); + lap_timestamps = calloc(lap_count, sizeof(double)); if (!session_data) { printf("Session data failed to allocate!"); - exit(ERR_MEMORY); + goto cleanup; } if (!lap_timestamps) { printf("Lap timestaps array failed to init!"); - exit(ERR_MEMORY); + goto cleanup; } duckdb_result lap_timestamps_res; if (duckdb_query(conn, "Select ts as DOUBLE from Lap\n", &lap_timestamps_res) == DuckDBError) { - exit(ERR_QUERY); + goto cleanup; } int current_index = 0; @@ -185,12 +184,19 @@ int main(int argc, char **argv) { } printf("\n"); - free(session_data); + // get the per laptime data + printf("Shutting down HardCompound!\n"); printf("Closing database and any connection.\n"); + goto cleanup; +exit: + return EXIT_OK; +cleanup: + free(NULL); + free(session_data); + free(lap_timestamps); duckdb_disconnect(&conn); duckdb_close(&db); - - return EXIT_OK; + goto exit; }