neko icon indicating copy to clipboard operation
neko copied to clipboard

Keep reference to k_db abstract and avoid early finalizer call

Open jonasmalacofilho opened this issue 4 years ago • 2 comments

In the SQLite CFFI library, connect returns an abstract Neko value, of k_db kind and with a finalizer set. If the k_result request value only keeps a pointer to the db struct, instead of the corresponding k_db value, the finalizer will be called as soon as no other external references remain to that connection, even if the request itself is still reachable.

The issue could manifest in code like (Haxe):

var rs = Sqlite.open("db.db").request("select * from tbl");
trace(rs.length);

Related: HaxeFoundation/haxe#8728

jonasmalacofilho avatar Sep 04 '19 19:09 jonasmalacofilho

Should we should check if val_db(r->database) == NULL since the database might have been closed in the meanwhile

ncannasse avatar Sep 05 '19 07:09 ncannasse

@ncannasse maybe, where exactly do you mean?

One thing I noticed is that free_db will set r->done early on, and that cleanly stops result_next. In fact, that's why we had no exception or segfault even though the db had been closed and freed.

jonasmalacofilho avatar Sep 05 '19 11:09 jonasmalacofilho

I was told to merge this!

Simn avatar Feb 08 '24 15:02 Simn