1. Tip of the week - Enhanced resources

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

new topic     » topic index » view message » categorize

2. Re: Tip of the week - Enhanced resources

Does this apply to allocate_string as well? As in a function:

function something(sequence s) 
atom x = allocate_string(s) 
-- do something with x 
return whatever 
end function 

No free(x) needed?

new topic     » goto parent     » topic index » view message » categorize

3. Re: Tip of the week - Enhanced resources

irv said...

Does this apply to allocate_string as well? As in a function:

function something(sequence s) 
atom x = allocate_string(s) 
-- do something with x 
return whatever 
end function 

No free(x) needed?

yes, no free, but you have to add TRUE for the cleanup param

function something(sequence s) 
 atom x = allocate_string(s,1) 
 -- do something with x 
 --free handled by euphoria 
 return whatever 
end function 
new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu