Skip to content

Commit 1c2210b

Browse files
committed
properly close database handles in all circumstances
1 parent 32b1b55 commit 1c2210b

4 files changed

Lines changed: 31 additions & 4 deletions

File tree

src/database.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,17 @@ void Database::Wrap(Handle<Object> handle) {
279279
handle_.MakeWeak(this, Destruct);
280280
}
281281

282+
inline void Database::MakeWeak (void) {
283+
handle_.MakeWeak(this, Destruct);
284+
}
285+
286+
void Database::Unref() {
287+
assert(!handle_.IsEmpty());
288+
assert(!handle_.IsWeak());
289+
assert(refs_ > 0);
290+
if (--refs_ == 0) { MakeWeak(); }
291+
}
292+
282293
void Database::Destruct(Persistent<Value> value, void *data) {
283294
Database* db = static_cast<Database*>(data);
284295
if (db->handle) {

src/database.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ class Database : public EventEmitter {
7777
}
7878

7979
~Database() {
80-
fprintf(stderr, "Calling destructor\n");
80+
assert(handle == NULL);
81+
fprintf(stderr, "Deleted Database\n");
8182
}
8283

8384
static Handle<Value> New(const Arguments& args);
@@ -95,6 +96,8 @@ class Database : public EventEmitter {
9596
static int EIO_AfterClose(eio_req *req);
9697

9798
void Wrap (Handle<Object> handle);
99+
inline void MakeWeak();
100+
virtual void Unref();
98101
static void Destruct (Persistent<Value> value, void *data);
99102
static int EIO_Destruct(eio_req *req);
100103
static int EIO_AfterDestruct(eio_req *req);

src/statement.cc

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ Handle<Value> Statement::New(const Arguments& args) {
100100
void Statement::EIO_BeginPrepare(Baton* baton) {
101101
assert(baton->db->open);
102102
assert(!baton->db->locked);
103-
// static_cast<PrepareBaton*>(baton)->stmt->Ref();
103+
static_cast<PrepareBaton*>(baton)->stmt->Ref();
104104
ev_ref(EV_DEFAULT_UC);
105105
fprintf(stderr, "Prepare started\n");
106106
eio_custom(EIO_Prepare, EIO_PRI_DEFAULT, EIO_AfterPrepare, baton);
@@ -191,9 +191,19 @@ void Statement::Wrap(Handle<Object> handle) {
191191
handle_.MakeWeak(this, Destruct);
192192
}
193193

194+
inline void Statement::MakeWeak (void) {
195+
handle_.MakeWeak(this, Destruct);
196+
}
197+
198+
void Statement::Unref() {
199+
assert(!handle_.IsEmpty());
200+
assert(!handle_.IsWeak());
201+
assert(refs_ > 0);
202+
if (--refs_ == 0) { MakeWeak(); }
203+
}
204+
194205
void Statement::Destruct(Persistent<Value> value, void *data) {
195206
Statement* stmt = static_cast<Statement*>(data);
196-
fprintf(stderr, "Auto-Finalizing handle started\n");
197207
if (stmt->handle) {
198208
eio_custom(EIO_Destruct, EIO_PRI_DEFAULT, EIO_AfterDestruct, stmt);
199209
ev_ref(EV_DEFAULT_UC);

src/statement.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class Statement : public EventEmitter {
5252

5353
~Statement() {
5454
fprintf(stderr, "Deleted Statement\n");
55+
assert(handle == NULL);
5556
db->pending--;
5657
Database::Process(db);
5758
db->Unref();
@@ -63,7 +64,9 @@ class Statement : public EventEmitter {
6364
static int EIO_AfterPrepare(eio_req *req);
6465

6566
void Wrap (Handle<Object> handle);
66-
static void Destruct (Persistent<Value> value, void *data);
67+
inline void MakeWeak();
68+
virtual void Unref();
69+
static void Destruct(Persistent<Value> value, void *data);
6770
static int EIO_Destruct(eio_req *req);
6871
static int EIO_AfterDestruct(eio_req *req);
6972

0 commit comments

Comments
 (0)