8000 sqlite memory leak on failed open · Issue #65 · SqliteModernCpp/sqlite_modern_cpp · GitHub
[go: up one dir, main page]

Skip to content
sqlite memory leak on failed open #65
Closed
@polesapart

Description

@polesapart

The sqlite::database constructor has a potential memory leak if opening the database fails for whatever reason:

database(std::u16string const & db_name): _db(nullptr) {
			sqlite3* tmp = nullptr;
			auto ret = sqlite3_open16(db_name.data(), &tmp);
			if(ret != SQLITE_OK) exceptions::throw_sqlite_error(ret);

			_db = std::shared_ptr<sqlite3>(tmp, [=](sqlite3* ptr) { sqlite3_close_v2(ptr); }); // this will close the connection eventually when no longer needed.

			//_db.reset(tmp, sqlite3_close); // alternative close. (faster?)
		}

accordingly to https://www.sqlite.org/c3ref/close.html one must close the handle even if open returned an error. The above can be fixed by moving the fail check and the exception throwing after defining the _db shared_ptr, so it will call sqlite3_close_v2 at stack unwinding. One can prefer to do an explicit close instead, which is cheaper but more verbose. I took the 1st approach, below:

database(std::u16string const & db_name): _db(nullptr) {
			sqlite3* tmp = nullptr;
			auto ret = sqlite3_open16(db_name.data(), &tmp);
			_db = std::shared_ptr<sqlite3>(tmp, [=](sqlite3* ptr) { sqlite3_close_v2(ptr); }); // this will close the connection eventually when no longer needed.
   			if(ret != SQLITE_OK) exceptions::throw_sqlite_error(ret);
			//_db.reset(tmp, sqlite3_close); // alternative close. (faster?)
		}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0