DWScript simplifies development by managing the lifecycle of script objects automatically. It uses a hybrid approach to ensure both performance and safety.
The primary mechanism for memory management is ARC. Every time an object is assigned to a variable or passed as a parameter, its reference count increases. When the variable goes out of scope, the count decreases.
When the count reaches zero, the object is immediately destroyed and its destructor is called. This provides deterministic cleanup for resources like file handles or database connections.
A common issue with pure ARC is "circular references" (e.g., Object A points to B, and B points back to A), which prevents reference counts from ever reaching zero.
DWScript includes a Garbage Collector (GC) that periodically scans for these isolated "islands" of objects. If an island is unreachable from the rest of the script, the GC will break the cycles and free the memory.
While automation is the default, you can still exercise manual control when necessary.
obj.Free to immediately trigger the destructor. Unlike some other environments, calling Free does not set the reference to nil. Instead, the object is marked as destroyed. Any subsequent attempt to access the object's fields or methods will trigger an Object already destroyed exception.Because of these mechanisms, DWScript is largely immune to common memory errors found in languages with manual management:
Free on an already destroyed object is safely handled.