Skip to content

fix: crash after closing the database #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/addon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,9 @@ Napi::Value Statement::Close(const Napi::CallbackInfo& info) {
auto env = info.Env();

auto stmt = FromExternal(info[0]);
if (stmt == nullptr) {
return Napi::Value();
}

int r = sqlite3_finalize(stmt->handle_);
if (r != SQLITE_OK) {
Expand All @@ -413,6 +416,10 @@ Napi::Value Statement::Run(const Napi::CallbackInfo& info) {
auto env = info.Env();

auto stmt = FromExternal(info[0]);
if (stmt == nullptr) {
return Napi::Value();
}

auto params = info[1];
auto result = info[2].As<Napi::Array>();

Expand Down Expand Up @@ -451,6 +458,10 @@ Napi::Value Statement::Step(const Napi::CallbackInfo& info) {
auto env = info.Env();

auto stmt = FromExternal(info[0]);
if (stmt == nullptr) {
return Napi::Value();
}

auto params = info[1];
auto cache = info[2];
auto is_get = info[3].As<Napi::Boolean>();
Expand Down
10 changes: 10 additions & 0 deletions test/memory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ test('db.close', () => {
db = new Database();
});

test('db.close with existing statement', () => {
const stmt = db.prepare('SELECT 1');
db.close();

expect(() => stmt.run()).toThrowError('Statement closed');

// Just to fix afterEach
db = new Database();
});

test('statement.close', () => {
const stmt = db.prepare('SELECT 1');
stmt.close();
Expand Down
Loading