Removing Objects created from external library and attached to root-pointers #9684
-
I have a micropython list object, which is set as a root-pointer and therefore excempt from garbage collection. The user can then call an api which returns another type of micropython object, which is stored in that list to avoid garbage collection. What I now need to do is handle when the user deletes that object. I know I need to call Any pointers appreciated! EDIT MORE INFO: using uPy v1.10, unable to upgrade yet so please don't suggest that as an answer I have a Micropython object (lets call it a DataStore object) that is created from a C module, that allows user to read a write data into. The Datastore object contains a C-allocated buffer for storing this data, and then another RTOS task can read and write into the same buffer externally from micropython. The list object is purely used as a root pointer to store the Datastore objects, it is not accessible from python. The user can then create any number of these Data store objects, and use them within their code. What I want to know is the best way to handle these objects being deleted. I can create a function called 'remove', which removes the object from the root list pointer like so: mp_obj_t mp_obj_new_datastore(mp_obj_t _name)
{
/* Create the uPy object. */
mp_obj_datastore_t *o = m_new_obj(mp_obj_datastore_t);
o->base.type = &mp_type_datastore;
o->name = _name;
memset(o->buffer, 0x00, sizeof(float32_t) * MAX_DEPTH);
int index = datastore_root_pointer_list->len;
mp_obj_list_append(datastore_root_pointer_list, o);
return upy_channels_store->items[index];
}
STATIC mp_obj_t datastore_channel_remove(mp_obj_t self_in) {
mp_obj_list_remove(datastore_root_pointer_list, self_in);
return mp_const_none;
} This leads me to I suppose two separate questions: How do I handle this object getting deleted with the del keyword from python, or going out of scope when created as a local variable and exiting the function, etc? In cpython I belive the del function is called when a function is deleted, is that true of Micropython too? How do I assign something to that? Once the object is removed from the list, how best should I free the memory allocated? Can I just free the memory like I would a normal struct in C, or do I have to do something special in micropython (like set the reference count to zero or something) because of the garbage collector and then let it free the memory? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 9 replies
-
I think you might need to explain in more detail what's going on. Is the list accessible from Python? What is the user applying the (I guess what I'm asking is, why do you need to manually make them reachable by the GC when they're being returned to Python which will do that for you by virtue of being referenced either by local variables or some other object. If you want some sort of cleanup for them when they do become unreachable, then you can use a finaliser)
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
Search the MicroPython source code for
__del__
for examples: wrap your datastore_channel_remove likeSTATIC MP_DEFINE_CONST_FUN_OBJ_1(datastore_channel_remove_obj, datastore_channel_remove);
then add thatdatastore_channel_remove_obj
to your locals dict with the nameMP_QSTR___del__
. It's similar to CPython.You don't do anything, and because your object isn't in the root pointers list anymore, gc will take care of it. Not that that is not a guarantee when or even if that will happen, but that is just how gc works.