diff options
author | Damien George <damien.p.george@gmail.com> | 2020-04-20 23:04:40 +1000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2020-05-02 16:08:04 +1000 |
commit | 73c58150f53d9d828c4fc8fb455cca6831eb8ddd (patch) | |
tree | 04e2c37efd92a1058291613e8c8108edfec36a41 /extmod/modbtree.c | |
parent | 391927c12634e3b80882499de5e92740c8472452 (diff) |
extmod/modbtree: Retain reference to underlying stream so it's not GC'd.
For ports that have a system malloc which is not garbage collected (eg
unix, esp32), the stream object for the DB must be retained separately to
prevent it from being reclaimed by the MicroPython GC (because the
berkeley-db library uses malloc to allocate the DB structure which stores
the only reference to the stream).
Although in some cases the user code will explicitly retain a reference to
the underlying stream because it needs to call close() on it, this is not
always the case, eg in cases where the DB is intended to live forever.
Fixes issue #5940.
Diffstat (limited to 'extmod/modbtree.c')
-rw-r--r-- | extmod/modbtree.c | 6 |
1 files changed, 4 insertions, 2 deletions
diff --git a/extmod/modbtree.c b/extmod/modbtree.c index 14dc8f348..2dd3a89b8 100644 --- a/extmod/modbtree.c +++ b/extmod/modbtree.c @@ -39,6 +39,7 @@ typedef struct _mp_obj_btree_t { mp_obj_base_t base; + mp_obj_t stream; // retain a reference to prevent GC from reclaiming it DB *db; mp_obj_t start_key; mp_obj_t end_key; @@ -65,9 +66,10 @@ void __dbpanic(DB *db) { mp_printf(&mp_plat_print, "__dbpanic(%p)\n", db); } -STATIC mp_obj_btree_t *btree_new(DB *db) { +STATIC mp_obj_btree_t *btree_new(DB *db, mp_obj_t stream) { mp_obj_btree_t *o = m_new_obj(mp_obj_btree_t); o->base.type = &btree_type; + o->stream = stream; o->db = db; o->start_key = mp_const_none; o->end_key = mp_const_none; @@ -361,7 +363,7 @@ STATIC mp_obj_t mod_btree_open(size_t n_args, const mp_obj_t *pos_args, mp_map_t if (db == NULL) { mp_raise_OSError(errno); } - return MP_OBJ_FROM_PTR(btree_new(db)); + return MP_OBJ_FROM_PTR(btree_new(db, pos_args[0])); } STATIC MP_DEFINE_CONST_FUN_OBJ_KW(mod_btree_open_obj, 1, mod_btree_open); |