Prevent Garbage collection from removing objects created by C-Libraries #9312
-
I am running into a Memory fault issue which appears to be caused by garbage collection. In the compiled C library, I have a definition for a Micropython object that contains a buffer, which can be written into by the uPy application, and accessed by another task external to Micropython: mp_obj_t mp_obj_new_msg_channel(mp_obj_t _name)
{
/* Create the uPy object. */
mp_obj_upy_channel_t *o = m_new_obj(mp_obj_msg_channel_t);
o->base.type = &mp_type_msg_channel;
o->name = _name;
memset(o->buffer, 0x00, sizeof(float32_t) * 64);
return MP_OBJ_FROM_PTR(o);
} However, garbage collection appears to be sometimes removing these items (despite them being declared globally in the uPy application) as the memory always fault happens roughly a second after garbage collection. It seems like I can set objects as Can I define an object type that the GC will not remove? or do I need to pre-allocate those objects at compile type to flag them as not used by the GC? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Maybe you find some tips in https://forum.micropython.org/viewtopic.php?f=2&t=10765&p=59215&hilit=garbage#p59215 ? |
Beta Was this translation helpful? Give feedback.
-
MICROPY_PORT_ROOT_POINTERS has been removed in favour of MP_REGISTER_ROOT_POINTER. It's much simpler because you no longer need to have mpconfigport.h know about your root pointers. See for example https://github.com/micropython/micropython/blob/master/ports/stm32/pin.c#L679 In your case where you have a list of objects, you should make your registered root pointer be a list (i.e. mp_obj_list_t) and then you can add/remove pointers to it as required. (Remember your objects don't need to be roots themselves, they just need to be reachable from a root, e.g. this list). You an also use |
Beta Was this translation helpful? Give feedback.
MICROPY_PORT_ROOT_POINTERS has been removed in favour of MP_REGISTER_ROOT_POINTER. It's much simpler because you no longer need to have mpconfigport.h know about your root pointers.
See for example https://github.com/micropython/micropython/blob/master/ports/stm32/pin.c#L679
In your case where you have a list of objects, you s…