[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: state of the Lua nation on resource cleanup
- From: David Manura <dm.lua@...>
- Date: Fri, 29 May 2009 21:17:27 -0400
On Thu, May 28, 2009 at 6:29 AM, Cosmin Apreutesei wrote:
> Aren't the variables (i.e bindings), and not the values (i.e. the
> acquired resources) that get out of scope? Shouldn't finalization
> occur whenever the last reference gets tossed? This would require a
> ref-counting mechanism (eg. like perl)...
Oftentimes, we want to define a resource's lifetime not dynamically
but lexically. That is, the resource shall be open at a point in the
code if and only if the code is located inside the variable's lexical
scope:
-- f is closed here
do
scoped f = acquire_thing()
-- f is open here
.....some code here.....
-- f is open here
end
-- f is closed here
Having invariants like these make a program easier to follow and
verify correctness.
> In such language, there should be no difference (from the RAII pov)
> between acquiring memory for a string and opening a file.
Unless we're dealing with very large memory acquisitions that could
fail, we often don't care about deterministic finalization for memory.
Not so for many other resources (e.g. things we might have only one
of).
>> What if it has any other references to it? For instance:
>>
>> with f = file.open('somefile') do some_global = f end ... should f be
>> closed by now? what would some_global hold now?
As Roberto said. Normally, that is a program error, like holding a
pointer to a stack object that goes out of scope in C. Don't do that.