I am reading Hodges book "More Coding in Delphi", section on Factory Pattern.
Trying to learn stuff. Broke down my code into small unit.
I use ReportMemoryLeaksOnShutDown := True;
and fallowing code produces me a memory leak. Why does it occur and how do I fix it?
unit Unit2;
interface
uses
Generics.Collections, System.SysUtils;
type
TGatewayTpe = (gtSwedbank, gtDNB);
type
TBaseGateway = class
end;
type
TSwedbankGateway = class(TBaseGateway)
end;
type
TGatewayFunction = reference to function: TBaseGateway;
type
TGatewayTypeAndFunction = record
GatewayType: TGatewayTpe;
GatewayFunction: TGatewayFunction;
end;
type
TGatewayFactory = class
strict private
class var FGatewayTypeAndFunctionList: TList<TGatewayTypeAndFunction>;
public
class constructor Create;
class destructor Destroy;
class procedure AddGateway(const AGatewayType: TGatewayTpe;
const AGatewayFunction: TGatewayFunction);
end;
implementation
class procedure TGatewayFactory.AddGateway(const AGatewayType: TGatewayTpe;
const AGatewayFunction: TGatewayFunction);
var
_GatewayTypeAndFunction: TGatewayTypeAndFunction;
begin
_GatewayTypeAndFunction.GatewayType := AGatewayType;
_GatewayTypeAndFunction.GatewayFunction := AGatewayFunction;
FGatewayTypeAndFunctionList.Add(_GatewayTypeAndFunction);
end;
class constructor TGatewayFactory.Create;
begin
FGatewayTypeAndFunctionList := TList<TGatewayTypeAndFunction>.Create;
end;
class destructor TGatewayFactory.Destroy;
begin
FreeAndNil(FGatewayTypeAndFunctionList);
end;
initialization
TGatewayFactory.AddGateway(
gtSwedbank,
function: TBaseGateway
begin
Result := TSwedbankGateway.Create;
end
);
end.