1. What is the reference count for memory
- Posted by ArthurCrump Aug 23, 2009
- 917 views
In the memory allocation routines there is an optional parameter 'cleanup'.
If this is set the memory is released when a reference count reaches zero.
How is the reference count increased and decreased?
Would the reference count reach zero when the allocated memory is used as a parameter to a function or procedure and the call is completed?
The following code is a Windows XP example.
include std/dll.e include std/machine.e atom lib, get_attr, Result sequence cmd cmd = command_line() -- Get a file for it to work on lib = open_dll("kernel32.dll") get_attr = define_c_func(lib, "GetFileAttributesA",{C_POINTER}, C_INT) -- The next line is the important bit. Result = c_func(get_attr,{allocate_string(cmd[2],1)}) -- Does Euphoria release this memory automatically when the call completes? -- The rest just confirms it got something include get.e printf(1,"%08x",Result) puts(1,"\nPress a key") if wait_key() then end if
2. Re: What is the reference count for memory
- Posted by mattlewis (admin) Aug 23, 2009
- 905 views
In the memory allocation routines there is an optional parameter 'cleanup'.
If this is set the memory is released when a reference count reaches zero.
How is the reference count increased and decreased?
Would the reference count reach zero when the allocated memory is used as a parameter to a function or procedure and the call is completed?
The following code is a Windows XP example.
No. The reference count of the variable is decremented when the program leaves the code block that contains the variable. The block could be the routine in which it is defined, or the if/while/for/loop block in which it is defined. The reference count is also decremented when you assign something else to that variable. Of course, if you assign the value of that variable to another variable, then the reference count is incremented.
Matt