Sto leggendo il libro di Hodges "Più codice in Delphi", sezione su Modello di fabbrica. Cercando di imparare cose. Rotto il mio codice in una piccola unità. Io uso ReportMemoryLeaksOnShutDown := True;
e il codice fallowing mi produce una perdita di memoria. Perché si verifica e come risolverlo?Modello di fabbrica, perdita di memoria
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.
Grazie per la nuova domanda, questo è proprio quello che ci piace qui –