Skip to content

Commit 00c79d8

Browse files
committed
fix more memleaks
1 parent 5bc9d68 commit 00c79d8

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

src/sqlite3db-add-data.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,11 @@ int rcpp_import_to_trip_table (const char* bikedb,
5151
{
5252
sqlite3 *dbcon;
5353
char *zErrMsg = nullptr;
54+
const char *zVfs = nullptr;
5455
size_t rc;
5556

56-
rc = static_cast <size_t> (sqlite3_open_v2(bikedb, &dbcon, SQLITE_OPEN_READWRITE, nullptr));
57+
rc = static_cast <size_t> (sqlite3_open_v2(bikedb, &dbcon, SQLITE_OPEN_READWRITE, zVfs));
58+
//rc = static_cast <size_t> (sqlite3_open(bikedb, &dbcon));
5759
if (rc != SQLITE_OK)
5860
throw std::runtime_error ("Can't establish sqlite3 connection");
5961

@@ -90,6 +92,7 @@ int rcpp_import_to_trip_table (const char* bikedb,
9092
sqlite3_prepare_v2(dbcon, sqlqry, BUFFER_SIZE, &stmt, nullptr);
9193

9294
sqlite3_exec(dbcon, "BEGIN TRANSACTION", nullptr, nullptr, &zErrMsg);
95+
sqlite3_free (zErrMsg);
9396

9497
for(int filenum = 0; filenum < datafiles.length(); filenum++)
9598
{
@@ -172,8 +175,10 @@ int rcpp_import_to_trip_table (const char* bikedb,
172175
}
173176
}
174177
}
178+
sqlite3_finalize(stmt);
175179

176180
sqlite3_exec(dbcon, "END TRANSACTION", nullptr, nullptr, &zErrMsg);
181+
sqlite3_free (zErrMsg);
177182

178183
if (city == "ny" || city == "bo" || city == "la" || city == "ph")
179184
import_to_station_table (dbcon, stationqry);
@@ -218,14 +223,19 @@ int rcpp_import_to_file_table (const char * bikedb,
218223
datafile_qry += std::to_string (nfiles) + ",\"" + city + "\",\"" +
219224
i + "\");";
220225

221-
rc = sqlite3_exec(dbcon, datafile_qry.c_str(), nullptr, nullptr, &zErrMsg);
226+
const char *sql = datafile_qry.c_str ();
227+
rc = sqlite3_exec(dbcon, sql, nullptr, nullptr, &zErrMsg);
228+
sqlite3_free (zErrMsg);
229+
//rc = sqlite3_exec(dbcon, datafile_qry.c_str(), nullptr, nullptr, &zErrMsg);
222230
if (rc == 0)
223231
nfiles++;
224232
}
225233

226234
rc = sqlite3_close_v2(dbcon);
235+
//rc = sqlite3_close(dbcon);
227236
if (rc != SQLITE_OK)
228237
throw std::runtime_error ("Unable to close sqlite database");
238+
sqlite3_free (zErrMsg);
229239

230240
return nfiles;
231241
}

src/sqlite3db-utils.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ int get_stn_table_size (sqlite3 * dbcon);
2828
int get_max_trip_id (sqlite3 * dbcon)
2929
{
3030
sqlite3_stmt * stmt;
31-
char qry_id [BUFFER_SIZE];
31+
char qry_id [BUFFER_SIZE] = "\0";
3232
int rc = sprintf(qry_id, "SELECT MAX(id) FROM trips");
3333
rc = sqlite3_prepare_v2(dbcon, qry_id, BUFFER_SIZE, &stmt, nullptr);
3434
rc = sqlite3_step (stmt);
3535
int max_trip_id = sqlite3_column_int (stmt, 0);
36-
sqlite3_reset (stmt);
36+
sqlite3_finalize (stmt);
3737
(void) rc; // supress unused variable warning;
3838

3939
return max_trip_id;
@@ -49,12 +49,12 @@ int get_max_trip_id (sqlite3 * dbcon)
4949
int get_max_stn_id (sqlite3 * dbcon)
5050
{
5151
sqlite3_stmt * stmt;
52-
char qry_id [BUFFER_SIZE];
52+
char qry_id [BUFFER_SIZE] = "\0";
5353
sprintf(qry_id, "SELECT MAX(id) FROM stations");
5454
int rc = sqlite3_prepare_v2(dbcon, qry_id, BUFFER_SIZE, &stmt, nullptr);
5555
rc = sqlite3_step (stmt);
5656
int max_stn_id = sqlite3_column_int (stmt, 0);
57-
sqlite3_reset (stmt);
57+
sqlite3_finalize (stmt);
5858
(void) rc; // supress unused variable warning;
5959

6060
return max_stn_id;
@@ -70,12 +70,12 @@ int get_max_stn_id (sqlite3 * dbcon)
7070
int get_stn_table_size (sqlite3 * dbcon)
7171
{
7272
sqlite3_stmt * stmt;
73-
char qry_id [BUFFER_SIZE];
73+
char qry_id [BUFFER_SIZE] = "\0";
7474
sprintf(qry_id, "SELECT COUNT(*) FROM stations");
7575
int rc = sqlite3_prepare_v2(dbcon, qry_id, BUFFER_SIZE, &stmt, nullptr);
7676
rc = sqlite3_step (stmt);
7777
int num_stns = sqlite3_column_int (stmt, 0);
78-
sqlite3_reset (stmt);
78+
sqlite3_finalize (stmt);
7979
(void) rc; // supress unused variable warning;
8080

8181
return num_stns;

0 commit comments

Comments
 (0)