Tip of the week - Enhanced resources
- Posted by ChrisB (moderator) Oct 31, 2010
- 1377 views
Brought to us by Matt Lewis
Euphoria 4.0 has added a mechanism that allows for automatic clean up of resources when their reference count drops to zero. Specifically:
delete_routine( routine_id ) and delete( object )
Since this relies on reference counting, it only applies to atoms and sequences. There are some built in and standard library routines that take advantage of this. In particular, open() and allocate(). For example:
procedure main() atom fn = open("my file.txt", "r", 1 ) -- the 1 tells euphoria to clean this up automatically! -- do stuff end procedure main()
When the reference count for fn goes to zero (probably when we return from main()), euphoria will automatically close the file. You can also supply your own cleanup routines:
procedure cleanup_my_object( object obj ) end procedure object foo = .... delete_routine( foo, routine_id("cleanup_my_object")) delete( foo ) -- cleanup_my_object( foo ) is called here!
Note that this did not wait for the reference count of the object assigned to foo to go to zero. It is possible to trigger cleanup explicitly by using the new built in delete() procedure.
Send your tips of the week to totw@openeuphoria.org
CategoryTip