Skip to content

Harden Database lifecycle: RAII connection close, no leak on init failure #27

@jkalias

Description

@jkalias

Problem

Database has no destructor that closes db_; the connection is closed only in Finalize(). The constructor (src/database.cc) calls sqlite3_open_v2 and then runs CreateTableQuery::Execute() for every registered record — if any of those throw, the already-opened db_ is leaked (no destructor runs to close it, and instance_ is never assigned).

Impact

  • A failure during initialization leaks the SQLite connection.
  • Relying on a manual Finalize() for cleanup is error-prone in general: forgetting it leaks the connection for the process lifetime.

Suggested direction

Give Database a destructor that closes db_ (RAII), and ensure the constructor closes db_ if initialization fails (or wrap the handle in an RAII type), so there is no leak on the error path and cleanup no longer depends on a manual call.

Already addressed (was part of this issue)

The original scope also included a null-check for Instance(). That was implemented in the thread-safety work (#9 / PR #28): Instance() now throws a clear error when the database is not initialized, guarded by the singleton lifecycle mutex. This issue is now scoped to the RAII / leak-on-init-failure concern only.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions