Search code examples
delphidynamic-arraysvariant

What is the best/preferred method for freeing a dynamic array of variants?


For instance, if I have the following:

type
  TVArray = array of array of Variant;
var
  MyArray : TVArray;

Then I add a bunch of elements to MyArray so that it has 1000 arrays of 30 variants each.

What is the best or preferred way to deallocate that memory if I want to do it right away?

MyArray := nil;

or

SetLength(MyArray, 0);

or

Finalize(MyArray);

or do I need to something to the sub arrays first? Does it matter what the individual variants contain?


Solution

  • All three are equivalent. Which you choose is a matter of style. I prefer the first one (assigning nil) because it's so succinct.

    The compiler knows how to release Variant values. There's nothing additional you need to do before you release the array — if you bother to release the array at all, given that the compiler will destroy it for you eventually anyway.