How to properly free records that contain various types in Delphi at once?

后端 未结 4 617
北海茫月
北海茫月 2020-11-29 19:47
type
  TSomeRecord = Record
    field1: integer;
    field2: string;
    field3: boolean;
  End;
var
  SomeRecord: TSomeRecord;
  SomeRecAr: array of TSomeRecord;
         


        
4条回答
  •  半阙折子戏
    2020-11-29 20:45

    With SomeRecord: TSomeRecord, SomeRecord will be an instance/variable of type TSomeRecord. With SomeRecord: ^TSomeRecord, SomeRecord will be a pointer to a instance or variable of type TSomeRecord. In the last case, SomeRecord will be a typed pointer. If your application transfer a lot of data between routines or interact with external API, typed pointer are recommended.

    new() and dispose() are only used with typed pointers. With typed pointers the compiler doesn't have control/knowlegde of the memory your application is using with this kind of vars. It's up to you to free the memory used by typed pointers.

    In the other hand, when you use a normal variables, depending on the use and declaration, the compiler will free memory used by them when it considers they are not necessary anymore. For example:

    function SomeStaff();
    var
        NativeVariable: TSomeRecord;
        TypedPointer: ^TSomeRecord;
    begin
        NaviveVariable.Field1 := 'Hello World';
    
        // With typed pointers, we need to manually
        // create the variable before we can use it.
        new(TypedPointer);
        TypedPointer^.Field1 := 'Hello Word';
    
        // Do your stuff here ...
    
        // ... at end, we need to manually "free"
        // the typed pointer variable. Field1 within
        // TSomerecord is also released
        Dispose(TypedPointer);
    
        // You don't need to do the above for NativeVariable
        // as the compiler will free it after this function
        // ends. This apply also for native arrays of TSomeRecord.
    end;
    

    In the above example, the variable NativeVariable is only used within the SomeStaff function, so the compiler automatically free it when the function ends. This appy for almost most native variables, including arrays and records "fields". Objects are treated differently, but that's for another post.

提交回复
热议问题