Dynamic arrays and memory management in Delphi

后端 未结 2 1278
南笙
南笙 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.

    0 讨论(0)
  • 2020-12-21 05:19

    Dynamic arrays are managed by the compiler. This is done by maintaining a reference count of all references to the array. When the last reference to the array is detached, the array is deallocated.

    The documentation says:

    Dynamic-array variables are implicitly pointers and are managed by the same reference-counting technique used for long strings. To deallocate a dynamic array, assign nil to a variable that references the array or pass the variable to Finalize; either of these methods disposes of the array, provided there are no other references to it. Dynamic arrays are automatically released when their reference-count drops to zero. Dynamic arrays of length 0 have the value nil. Do not apply the dereference operator (^) to a dynamic-array variable or pass it to the New or Dispose procedure.

    In your example, assigning nil to your variable detaches the one and only reference and results in the array being deallocated. So there is no leak.

    Delphi dynamic arrays are very different from C++ new. The closest analogue to that in Delphi is raw memory allocation with GetMem or New.


    Your edit asks a different question. Instances of classes are not managed. They must be explicitly be freed. Your code does that. There is no double free because the compiler does not manage instances of classes.

    0 讨论(0)
提交回复
热议问题