Skip to content

Commit

Permalink
avoid memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
iwongu committed Nov 5, 2023
1 parent 947c473 commit b7e2f8b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
7 changes: 6 additions & 1 deletion headeronly_src/sqlite3pp.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,13 @@ namespace sqlite3pp
{
if (dbname) {
auto rc = connect(dbname, flags, vfs);
if (rc != SQLITE_OK)
if (rc != SQLITE_OK) {
// Whether or not an error occurs when it is opened, resources
// associated with the database connection handle should be released
// by passing it to sqlite3_close() when it is no longer required.
disconnect();
throw database_error("can't connect database");
}
}
}

Expand Down
13 changes: 12 additions & 1 deletion test/testdb.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// $ g++ testdb.cpp -I ../headeronly_src -lsqlite3 --std=c++11 -Wno-deprecated;
// $ g++ testdb.cpp -I ../headeronly_src -lsqlite3 --std=c++11
// $ g++ testdb.cpp -I ../headeronly_src -lsqlite3 --std=c++17

#include <iostream>
#include "sqlite3pp.h"
Expand Down Expand Up @@ -220,6 +221,15 @@ void test_aggregate() {
expect_eq(16, (*iter).get<int>(1));
}

void test_invalid_path() {
try {
sqlite3pp::database db("/test/invalid/path");
} catch (sqlite3pp::database_error& e) {
return;
}
expect_true(false);
}

int main()
{
test_insert_execute();
Expand All @@ -236,6 +246,7 @@ int main()
test_function();
test_function_args();
test_aggregate();
test_invalid_path();
}

sqlite3pp::database contacts_db() {
Expand Down

0 comments on commit b7e2f8b

Please sign in to comment.