John John
John John

Reputation: 373

Do string variables in records get freed when the record goes out of scope?

In Delphi you can declare a record containing a String. This means the record just contains a pointer the heap when the string is located; does the heap memory get freed when this record goes out of scope? Where would I find a documentation on such behavior?

Upvotes: 2

Views: 151

Answers (2)

Remy Lebeau
Remy Lebeau

Reputation: 597941

Strings are managed types (which includes strings, dynamic arrays, interfaces, and variants). The compiler is responsible for allocating and freeing managed types in all contexts, including in records.

So yes, the compiler will emit code to free the string when the record goes out of scope.

Upvotes: 6

Andreas Rejbrand
Andreas Rejbrand

Reputation: 109003

Yes, strings are reference counted and the heap objects are freed when the reference count reaches zero, which happens when the last reference to the string goes out of scope. This includes references that are object and record members.

Upvotes: 4

Related Questions