Finalization in Dynamic Native Modules (.mpy) #17197
Replies: 4 comments 12 replies
-
Hi @dubiousjim . Cool that more are using dynamic native modules!
What kind of resources are you looking too free? If it is just about allocated memory or similar, I believe you should use just del. This should get called when an object gets garbage collected. So usually a little bit after it goes out of scope. I am pretty sure that using mp_obj_malloc_with_finaliser is not needed to have this happen. |
Beta Was this translation helpful? Give feedback.
-
The build of a dynamic module is entirely separate from the MicroPython firmware build. So the symbols (and thus functions) that are available to dynamic native modules are separate. That is why you do not have mp_obj_malloc_with_finaliser_helper. Only symbols that are in the explicitly created symbol table are available. |
Beta Was this translation helpful? Give feedback.
-
I've now managed to get finalisers to work with Dynamic Native Modules. Thanks @jonnor for pointing me in the right direction. This did require making some changes to the interpreter/runtime, and recompiling. Based on the source for MicroPython 1.25.0, testing with the Unix port (specifically testing on Linux with musl libc), here are the changes I needed to make to the interpreter:
Note the commented out entry at the end of the If the interpreter/runtime is compiled like that, with the macro
Note I had to add With these changes, then here is a session demonstrating the finaliser works:
|
Beta Was this translation helpful? Give feedback.
-
I'm working on this for exactly this kind of purpose: #16063 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm experimenting with extending Micropython via .mpy files, and want to provide finalizers to release resources (as a fallback if some extension objects aren't properly
__exit__
ed orclose
d). As a test of the basic finalizer functionality, I'm trying to modifyexamples/natmod/features4
.I'm using the Unix port, but I'd like to understand what's needed and how things work in a general way.
Here's how I started:
That builds and links fine, but the finalizer never triggers. (I import the .mpy file into micropython, created one of the objects bound to a function's local variable, exit the function, do a
gc.collect()
several times.)Some testing revealed that on my configuration, MICROPY_ENABLE_FINALISER wasn't defined, at least when I was compiling the .mpy file. So I added:
to the extension's Makefile. But now linking fails with
undefined symbol: mp_obj_malloc_with_finaliser_helper
.I tried copying the definition of that function into my extension, but then linking just fails a further step down the road, with
undefined symbol: m_malloc_with_finaliser
. I didn't expect that was the right solution anyway.I've been reading through the Micropython source tree, this forum, and elsewhere. But at the moment, I'm confused enough that I thought it'd be best to ask for help.
__del__
dunder method on the object, and making sure that the object is allocated withmp_obj_malloc_with_finaliser
), I've seen some reference to "a deinit function," in a way that suggested this might be a special slot on the object, that worked independently of the__del__
mechanism. Is this right? If so how do I use it?__del__
method, do I need to make sure that my micropython runtime was compiled in a special way (specifically, withMICROPY_ENABLE_FINALISER
defined) in order for the finalization infrastructure to be present?MICROPY_ENABLE_FINALISER
seems to give me linking errors, before the .mpy file itself is created, and so before it gets the opportunity to see what is or isn't available in the runtime.Beta Was this translation helpful? Give feedback.
All reactions