73c58150f5
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.
24 lines
690 B
Python
24 lines
690 B
Python
# Test btree interaction with the garbage collector.
|
|
|
|
try:
|
|
import btree, uio, gc
|
|
except ImportError:
|
|
print("SKIP")
|
|
raise SystemExit
|
|
|
|
N = 80
|
|
|
|
# Create a BytesIO but don't keep a reference to it.
|
|
db = btree.open(uio.BytesIO(), pagesize=512)
|
|
|
|
# Overwrite lots of the Python stack to make sure no reference to the BytesIO remains.
|
|
x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
|
|
|
# Write lots of key/value pairs, which fill up the DB and also allocate temporary heap
|
|
# memory due to the string addition, and do a GC collect to verify that the BytesIO
|
|
# is not collected.
|
|
for i in range(N):
|
|
db[b"thekey" + str(i)] = b"thelongvalue" + str(i)
|
|
print(db[b"thekey" + str(i)])
|
|
gc.collect()
|