当TRttiProperty.SetValue与TMemoryStream属性一起使用时,如何避免内存泄漏?

我用SourceProperties[0].SetValue(lObject, lStream)设置MemoryStream : TMemoryStream的属性lObject

如果我随后释放了lStream, 的MemoryStream属性lObject似乎被设置为nil

如果我不释放lStream,就会出现内存泄漏。

通过 RTTI将 分配lStreamMemoryStream属性的正确方法是什么?

下面列出了完整的示例代码:

unit Unit28;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics,
  FMX.Controls.Presentation, FMX.StdCtrls, System.Rtti;

type
  TMyObject = Class(TObject)
  private
    pMemoryStream : TMemoryStream;
  published
    property MemoryStream : TMemoryStream read pMemoryStream  write pMemoryStream;
    constructor Create;
    destructor  Destroy;override;
  end;

type
  TForm28 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form28: TForm28;

implementation


{$R *.fmx}

constructor TMyObject.Create;
begin
  pMemoryStream := TMemoryStream.Create;
end;

destructor TMyObject.Destroy;
begin
  pMemoryStream.DisposeOf;
  inherited;
end;


////Problematic when using RTTI
procedure TForm28.Button1Click(Sender: TObject);
var
  SourceRttiContext : TRttiContext;
  SourceRttiType : TRttiType;
  SourceProperties: TArray<TRttiProperty>;
  lStream : TMemoryStream;
  lObject : TObject;
begin
  lObject := TMyObject.Create;
  SourceRttiType := SourceRttiContext.GetType(lObject.ClassType);
  SourceProperties := SourceRttiType.GetProperties;
  lStream := TMemoryStream.Create;
  lStream.LoadFromFile('C:UsersPublicPicturesSample PicturesDesert.jpg');
  if (SourceProperties[0].PropertyType.ToString = 'TMemoryStream') then
  begin
    SourceProperties[0].SetValue(lObject, lStream);
  end;
  lStream.DisposeOf;
  TMyObject(lObject).MemoryStream.SaveToFile('C:UsersPublicPicturesSample PicturesTest1.jpg');
  lObject.DisposeOf;
end;


////No problems when not using RTTI
procedure TForm28.Button2Click(Sender: TObject);
var
  lStream : TMemoryStream;
  lObject : TMyObject;
begin
  lObject := TMyObject.Create;
  lStream := TMemoryStream.Create;
  lStream.LoadFromFile('C:UsersPublicPicturesSample PicturesDesert.jpg');
  lObject.MemoryStream.LoadFromStream(lStream);
  lStream.DisposeOf;
  lObject.MemoryStream.SaveToFile('C:UsersPublicPicturesSample PicturesTest1.jpg');
  lObject.DisposeOf;
end;

end. 

回答

您不仅有泄漏,而且还在使用死内存流对象。

问题是,当您设置MemoryStream属性时,您直接将其引用设置为指向本地创建的lStream对象。您没有创建副本,因此您正在泄漏在TMyObject构造函数中创建的内存流实例。

我不确定在您的示例中使用 RTTI 的目的是什么,最佳解决方案取决于您要实现的目标,这并不完全清楚。

如果您需要通过 RTTI 设置流,那么您需要实现属性设置器以避免泄漏。

  TMyObject = Class(TObject)
  private
    pMemoryStream : TMemoryStream;
    procedure SetMemoryStream(const Value: TMemoryStream);
  published
    property MemoryStream: TMemoryStream read pMemoryStream write SetMemoryStream;
    constructor Create;
    destructor  Destroy; override;
  end;

procedure TMyObject.SetMemoryStream(const Value: TMemoryStream);
begin
  pMemoryStream.CopyFrom(Value, 0);
end;

var
  SourceRttiContext : TRttiContext;
  SourceRttiType : TRttiType;
  SourceProperties: TArray<TRttiProperty>;
  lStream : TMemoryStream;
  lObject : TObject;
begin
  lObject := TMyObject.Create;
  try
    SourceRttiType := SourceRttiContext.GetType(lObject.ClassType);
    SourceProperties := SourceRttiType.GetProperties;
    lStream := TMemoryStream.Create;
    try
      lStream.LoadFromFile('C:UsersPublicPicturesSample PicturesDesert.jpg');
      if (SourceProperties[0].PropertyType.ToString = 'TMemoryStream') then
      begin
        SourceProperties[0].SetValue(lObject, lStream);
      end;
      TMyObject(lObject).MemoryStream.SaveToFile('C:UsersPublicPicturesSample PicturesTest1.jpg');
    finally
      lStream.Free;
    end;
  finally
    lObject.Free;
  end;
end;

但是,创建副本将使用更多内存。为避免这种情况,解决方案之一是将本地创建的流的所有权转移到TMyObject. 在这种情况下,您不应lStream在本地发布。由于所有权转移使异常处理复杂化,因此处理双重释放的最简单方法是在转移完成后设置lStreamnil,并且TMyObject实例将负责释放内存流。

procedure TMyObject.SetMemoryStream(const Value: TMemoryStream);
begin
  pMemoryStream.Free;
  pMemoryStream := Value;
end;

var
  SourceRttiContext : TRttiContext;
  SourceRttiType : TRttiType;
  SourceProperties: TArray<TRttiProperty>;
  lStream : TMemoryStream;
  lObject : TObject;
begin
  lObject := TMyObject.Create;
  try
    SourceRttiType := SourceRttiContext.GetType(lObject.ClassType);
    SourceProperties := SourceRttiType.GetProperties;
    lStream := TMemoryStream.Create;
    try
      lStream.LoadFromFile('C:UsersPublicPicturesSample PicturesDesert.jpg');
      if (SourceProperties[0].PropertyType.ToString = 'TMemoryStream') then
        begin
          SourceProperties[0].SetValue(lObject, lStream);
          // ownership transfer is successful nil local reference to
          // avoid double free
          lStream := nil;
        end;
      TMyObject(lObject).MemoryStream.SaveToFile('C:UsersPublicPicturesSample PicturesTest1.jpg');
    finally
      lStream.Free;
    end;
  finally
    lObject.Free;
  end;
end;

但是这里的主要问题仍然是为什么首先使用本地创建的内存流,而不是直接使用TMyObject内存流实例加载图像?


以上是当TRttiProperty.SetValue与TMemoryStream属性一起使用时,如何避免内存泄漏?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>