micropython-usermod icon indicating copy to clipboard operation
micropython-usermod copied to clipboard

request: C finalizers

Open pmp-p opened this issue 6 years ago • 6 comments

First thank for the huge work already done !

regarding changelog : - misc.h: add m_new_obj_var_with_finaliser()

and :

https://github.com/micropython/micropython/issues/245

in help to fix : https://github.com/micropython/micropython/issues/1878

i think an addition on how to use __del__ C finalizer with m_new_obj_var_with_finaliser would be great.

pmp-p avatar Sep 24 '19 09:09 pmp-p

Sorry, somehow I have overlooked the issue. But it is not quite clear to me, what exactly is requested here. Could you, please, elaborate a bit?

v923z avatar Dec 02 '19 18:12 v923z

the C __del__ method is actually undocumented, the code demonstrated by @stinos in https://github.com/micropython/micropython/issues/1878#issuecomment-534911108 is enough to cover its use and implement finalizers for a usermod ( i've tested the snippet on unix port, it works very well).

it allows garbage collections for user created objects without hooking into root pointers ( internals, a bit messy and subject to changes ). On the other hand m_new_obj_var_with_finaliser() is likely to stay in the future.

pmp-p avatar Dec 03 '19 15:12 pmp-p

Thanks for the clarification! I can include that in the documentation, no problem. Would the discussion on classes be a good place for that?

Actually, if you feel like it, you can also send a pull request.

v923z avatar Dec 03 '19 19:12 v923z

the C __del__ method is actually undocumented, the code demonstrated by @stinos in micropython/micropython#1878 (comment) is enough to cover its use and implement finalizers for a usermod ( i've tested the snippet on unix port, it works very well).

it allows garbage collections for user created objects without hooking into root pointers ( internals, a bit messy and subject to changes ). On the other hand m_new_obj_var_with_finaliser() is likely to stay in the future.

I must admit, I am still a bit confused: the snippet that you quoted is a modification of the garbage collector code, so, certainly not part of a user-defined module. On the other hand, your second comment indicates otherwise. In other words, are you saying that the __del__ special method of a class should be discussed somewhere in the manual? The discussion on the cited thread was rather diverse, thus, I am not so sure, what exactly pertains to the issue at hand.

v923z avatar Jan 03 '20 21:01 v923z

I must admit, I am still a bit confused: the snippet that you quoted is a modification of the garbage collector code, so, certainly not part of a user-defined module.

In the example code I posted there, functionality for a callback for a finalizer gets registered as part of the gc module but it doesn't have to be: it could also be part of a custom C module so without modifications to modgc.c. I probably just did it that way because it was easier to test.

stinos avatar Jan 04 '20 18:01 stinos

any update on this? I'm a usermod noob and don't understand how usermods can work reliably without del functions to clean up peripherals & other state upon a soft reboot (otherwise interrupts continue to call callbacks that no longer exist and crash the system).

moefear85 avatar Nov 06 '23 12:11 moefear85