lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I fully agree. Even in C++ and Java that have weak references, as soon as the reference is no longer used, the object can be garbage collected

(well, weak references can even be collected before the variable goes out of scope, and even if it is accessed later in the same scope, in which case there will be an exception or wrapper to reconstruct it, this is typically for objects that are local caches of data infered from a remote object, or which is slow to load via some I/O or recomputing).

Java and C/C++ compilers also perform "deletion" of unused variables, by reusing their storage slots for other variables. That's the purpose of the data flow analysis performed by the compiler and it saves a lot of run time space. Only a unoptimized version for debugging (or an interpreted version based on reflection, because one inserted a variable tracker in the debugger and the programmer wants to preserve their value somewhere in a scratchpad of the debugger) will preserve these unused slots. But debuggers today are smart enough to use the variable scoping information generated by the compiler and no longer need to preserve the value of all variables that are only in syntaxic scope in the source file (the debugger may still have those variables in their own scratchpad, showing also that they are no longer accessible and possibly displaying only the last value they had before getting out of runtime scope becaue there are other unrelated variables reusing their former storage)


Le lun. 3 juin 2019 à 04:54, Gé Weijers <ge@weijers.org> a écrit :
Francisco,

The idea that every object reference must be assigned a slot that lasts until the end of a block/call is not required anywhere outside of the C interface of the PUC Lua implementation, you're making unwarranted assumptions about how things should work.

An object reference may be abandoned as soon as the program gets to the point where it will no longer need the object for its normal execution (i.e. ignoring the effects of weak references and finalizers). The finalizer can run any time after that (1ns or 1 year later).

Using finalizers to modify the state of your program outside of the object that became unreachable is going to end in tears, unless you are very careful, you end up with a program whose behavior will be difficult to analyze because the finalizer can run at almost any point in your program, not just at statement boundaries.