Dynamic arrays and memory management in Delphi

后端 未结 2 1289
南笙
南笙 2020-12-21 04:53

The following article on dynamic arrays in Delphi says that you allocate a dynamic array using the SetLength() function.

myObjects : array of My         


        
2条回答
  •  北海茫月
    2020-12-21 05:15

    Setting an object reference to nil does NOT free it. It simply decrements the internal reference count. When that reference count hits 0, the memory is freed or available for freeing.

    So doing:

    Obj.Free
    Obj := nil;
    

    isn't going to free it twice at all. It's going to free it once and set the Obj pointer to null.

    For strings, the reference count used to be stored a an offset of 2 words before the first element and the size was stored 1 word before the first element. If it is constant, the reference count was usually -1. Not sure if this is still the case.

提交回复
热议问题