Search code examples
delphimemorydestructorprimitivedelphi-10-seattle

Is TPoint a primitive in Delphi?


I am creating several TPoint objects at runtime but I am not destroying them.

I checked the code of TPoint in System.Types:

PPoint = ^TPoint;
  TPoint = record
    X: FixedInt;
    Y: FixedInt;
  public
    constructor Create(P : TPoint); overload;
    constructor Create(const X, Y : Integer); overload;

    //operator overloads
    class operator Equal(const Lhs, Rhs : TPoint) : Boolean;
    class operator NotEqual(const Lhs, Rhs : TPoint): Boolean;
    class operator Add(const Lhs, Rhs : TPoint): TPoint;
    class operator Subtract(const Lhs, Rhs : TPoint): TPoint;

    class operator Implicit(Value: TSmallPoint): TPoint;
    class operator Explicit(Value: TPoint): TSmallPoint;

    class function PointInCircle(const Point, Center: TPoint; const Radius: Integer): Boolean; static; inline;
    /// <summary> Zero point having values of (0, 0). </summary>
    class function Zero: TPoint; inline; static;

    function Distance(const P2 : TPoint) : Double;

    procedure SetLocation(const X, Y : Integer); overload;
    procedure SetLocation(const P : TPoint); overload;
    procedure Offset(const DX, DY : Integer); overload;
    procedure Offset(const Point: TPoint); overload;
    function Add(const Point: TPoint): TPoint;
    function Subtract(const Point: TPoint): TPoint;
    function IsZero : Boolean;

    function Angle(const APoint: TPoint): Single;
  end;

By reading it i see there is no destructor and moreover it is a record of primitives. I do not master Delphi at a point to be sure about it, but i think it is not needed to call MyPoint.Free. May some expert confirm?


Solution

  • TPoint is not a class.

    What convinced me is that I was trying to do an objectlist defined like this:

    uses Generics.Collections;
    //
    PointsList: TObjectList<TPoint>;
    

    and the compiler told me

    "E2511 Type parameter 'T' must be a class type"

    so this convinced me that TPoint is not an object and therefore must not be freed. Moreover about storing TPoint in a List this thread describes a better use of Generics with TList: store array of TPoint inside TObjectList. In fact a list of TPoint is safely defined as TList<TPoint>.

    Moreover I tried to search leaks with Eurekalog and non freed TPoint is not giving leak as a non freed TStringList does.

    By the way, also the fact that TPoint is defined in System.Types could have been replied to my question "Is TPoint a primitive in Delphi?"