Re: find options

new topic     » goto parent     » topic index » view thread      » older message » newer message
jeremy said...

Let's say you have wrapped PCRE. When you call pcre:new("[A-Z]+") memory is allocated and returned into euphoria as an atom. Take this example:

Here is, perhaps, a more common example:

type auto_file( atom af ) 
    return af >= 0 
end type 
 
procedure cleanup_autofile( auto_file af ) 
    close( af ) 
end procedure 
cleanup_type( auto_file, routine_id("cleanup_autofile")) 
 
function new_autofile( sequence fname, sequence mode ) 
    auto_file af = open( fname, mode ) 
    -- tell the interpreter to clean this up for us 
    return bless_type( auto_file, af ) 
end function 
 
procedure main() 
    auto_file fn = open( "readme.txt", "r") 
    sequence line = gets(fn) 
    puts( 1, line ) 
    -- fn gets de-referenced here and cleanup_autofile() is called. 
end procedure 
main() 

Again, consider this to be pseudocode. The syntax for auto-cleanup of UDTs hasn't been decided yet. But the key is that once the auto_file variable goes completely out of scope, its cleanup routine will be called.

In this case, we don't have to remember to close the file handle, but this can work for other types, as well, including data that manually allocates memory.

Matt

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu