pythoncoder wrote: ↑Fri Nov 17, 2017 10:58 am
The gc will de-allocate a memory block if there is no Python reference to it which is currently in scope. So I think you need a Python function which allocates the RAM and returns a reference to it, which you can store in a variable.
Have you considered using the official framebuf module?
Notice the comment
// need to store this to prevent GC from reclaiming buf
How does storing that prevent GC from reclaiming the memory?
modframebuf.c
Code: Select all
typedef struct _mp_obj_framebuf_t {
mp_obj_base_t base;
mp_obj_t buf_obj; // need to store this to prevent GC from reclaiming buf
void *buf;
uint16_t width, height, stride;
uint8_t format;
} mp_obj_framebuf_t;
It's just referenced once in
Code: Select all
STATIC mp_obj_t framebuf_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
mp_arg_check_num(n_args, n_kw, 4, 5, false);
mp_obj_framebuf_t *o = m_new_obj(mp_obj_framebuf_t);
o->base.type = type;
o->buf_obj = args[0];
...
With respect to Python code, we're storing the pointer to the
bytearray(10 * 100 * 2)
Code: Select all
fbuf = FrameBuffer(bytearray(10 * 100 * 2), 10, 100, framebuf.RGB565)
So how is storing it preventing it from being GCd?