Myles Penlington asked of the group:

> DynamicArray : array of IMyInterface;
>
> >From my understanding if you do SetLength(DynamicArray, 20) then
> the array
> is uninitialised which is real bad when dealing with interfaces.

The Delphi documentation is completly *wrong* when it states that the memory
for dynamic arrays is unitialised when you size (or resize) the array. The
memory for the new length is carefully initialised to all 0 bytes, and so in
your case you will have an array of nil pointers to interfaces.

Just thinking about how the dynamic arrays are implemented means that the
memory absolutely has to be initialised. Local dynamic arrays are *always*
destructed cleanly in the procedure epilogue (as well as strings and
interfaces) by using the System._VarClear procedure which in turn will
destruct each of the elements in the array.

Now because you can declare dynamic arrays that include other dynamic
arrays, strings, interfaces and objects means that if the memory wasn't
initialised then the internal memory management would blow up as soon as you
sized you first array containing any dynamic delihi type and left a
procedure.

The same applies to dynamic arrays declared in classes, as the object
destructor code carefully cleans up strings and dynamic arrays when an
object is destroyed.

Cheers, Max.


---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz

Reply via email to