Memory (and resource locks) are returned to the OS at deterministic points during a program's execution. The control flow of a program by itself is enough to know where, for sure, a given resource can be deallocated. Just like how a human programmer knows where to write
fclose(file) when the program is done with it.
GCs solve this by figuring it out directly during runtime when the control flow is executed. But the real source of truth about the control flow is the source. So theoretically, it should be possible to determine where to insert the
free() calls before compilation by analyzing the source (or AST).
Reference counting is an obvious way to implement this, but it's easy to encounter situations where pointers are still referenced (still in scope) yet no longer needed. This just converts the responsibility of manually deallocating pointers to a responsibility to manually manage the scope/references to those pointers.
It seems like it's possible to write a program that can read a program's source and:
- predict all the permutations of the program's control flow---to similar accuracy as watching the live execution of the program
- track all the references to allocated resources
- for each reference, traverse the whole subsequent control flow in order to find the earliest point that the reference is guaranteed to never be dereferenced
- at that point, insert a deallocation statement at that line of source code
Is there anything out there that does this already? I don't think Rust or C++ smart pointers/RAII is the same thing.