I know what normal global variables and local variables are, but what are "local reference-counted" variables and "local non reference-counted" variables?
What is it? What's the difference? How do they work?
Can someone show code examples and explain please?
A reference-counted object maintains an internal counter of how many variables refer to it at runtime. Delphi native types that have reference counting include the string types
AnsiStringandUnicodeString, as well asinterfaces and dynamic arrays.Such types are automatically managed by the Delphi compiler for you. Multiple variables of these types can refer to the same object in memory. When a managed variable refers to an existing object, the compiler increases 1 the object's counter. When that variable no longer refers to the object, because it was reassigned or went out of scope, the compiler decreases 1 the object's counter.
1: unless the variable is
const, then it doesn't.When an object's counter falls to zero, meaning no more variables refer to it, then the compiler frees the object from memory.