델파이에서 싱글턴 메모리 누수, Create 문제 없이 구현하기

unit Singleton;

interface

uses SysUtils;

type
    TSingleTon = class
    private
        procedure FreeSingletonInstance;
        procedure CreateSingletonInstance;
        class var Instance: TSingleTon;
    public
        class procedure Create;
        class procedure Free;
        class function GetInstance: TSingleTon;
    end;

implementation

class procedure TSingleTon.Create;
begin
    raise ENoConstructException.Create('It''s a singleton class!');
end;

class procedure TSingleTon.Free;
begin
    raise EInvalidOpException.Create('It''s a singleton class!');
end;

procedure TSingleTon.FreeSingletonInstance;
begin
    //Write destructor here
    inherited Free;
end;

procedure TSingleTon.CreateSingletonInstance;
begin
    //Write constructor here
end;

class function TSingleTon.GetInstance: TSingleTon;
begin
    if Instance = nil then
    begin
        result := inherited Create as self;
        result.CreateSingletonInstance;
    end
    else
    result := Instance;
end;

initialization
finalization
    TSingleTon.Instance.FreeSingletonInstance;
end.