客户端应用程序和 DataSnap 服务器的原型(prototype)。我想将TObjectList从服务器传输到客户端。
这是有效的,但是我传送的所有对象都保留在服务器和客户端的内存中。
我做错了什么?
生命周期= session
TPessoa 对象实现另一个类 (TConta) 的 TObjectList:
TConta = class(TObject)
private
FBanco: string;
FConta: Integer;
FAgencia: Integer;
procedure SetAgencia(const Value: Integer);
procedure SetBanco(const Value: string);
procedure SetConta(const Value: Integer);
published
property Banco : string read FBanco write SetBanco;
property Agencia : Integer read FAgencia write SetAgencia;
property Conta : Integer read FConta write SetConta;
end;
TContasCollection = TObjectList<TConta>;
TPessoa = class(TObject)
private
FContas: TContasCollection;
FId: Integer;
FNome: string;
procedure SetContas(const Value: TContasCollection);
procedure SetId(const Value: Integer);
procedure SetNome(const Value: string);
published
property Id : Integer read FId write SetId;
property Nome : string read FNome write SetNome;
property Contas : TContasCollection read FContas write SetContas;
end;
ServerMetodsUnit 中的公共(public)方法:
function getPessoa(id : Integer) : Tpessoa;
function TServerMethods1.getPessoa(id: Integer): Tpessoa;
begin
result := Tpessoa.create;
result.id := id;
result.nome := 'NoName';
result.contas := getContas;
end;
function TServerMethods1.getContas: TContasCollection;
var conta : TConta;
begin
conta := TConta.Create;
conta.Banco := 'CEF';
conta.Agencia := 1;
conta.Conta := 123;
Result := TContasCollection.Create();
Result.Add(conta);
end;
客户:
procedure TForm2.btn1Click(Sender: TObject);
var pessoa : Tpessoa;
begin
pessoa := ClientModule1.ServerMethods1Client.getPessoa(1);
mmo1.Lines.Add(pessoa.Nome);
mmo1.Lines.Add(pessoa.Contas[0].Banco);
end;
结果正确,但是内存泄漏消息显示在服务器和客户端中 (System.ReportMemoryLeaksOnShutdown := true;):
Unexpected Memory Leak An unexpected memory leak has occurred. The unexpected small block leaks are:
1 - 12 bytes: TMoveArrayManager x 1, Unknown x 1 13 - 20 bytes: TConta x 1, UnicodeString x 1 37 - 44 bytes: TObjectList x 1
如何解决这个内存泄漏而又不影响服务?
最佳答案
发生内存泄漏是因为您没有释放正在创建的对象。
首先,您要创建 pessoa
的客户端部分btn1Click
中的对象作为局部变量,但您没有释放它。
procedure TForm2.btn1Click(Sender: TObject);
var pessoa : Tpessoa;
begin
pessoa := ClientModule1.ServerMethods1Client.getPessoa(1);
mmo1.Lines.Add(pessoa.Nome);
mmo1.Lines.Add(pessoa.Contas[0].Banco);
pessoa.Free; // you are no longer using pessoa object after that point so release it
end;
TPessoa
类(class)有FContas
我们看不到如何创建或释放的字段,也许您只是省略了代码,也许它根本不存在。无论如何,FContas
也已在某个时候发布。如果TPessoa
类(class)是 FContas
的所有者字段(看起来应该是)那么你必须将析构函数添加到 TPessoa
您可以免费上课FContas
收藏。
FContas
是 TObjectList
默认情况下拥有添加的对象,并且这些对象将在 FContas
时释放已发布。
也可能存在 FContas
的泄漏SetContas
中的对象方法,但如果不知道代码的外观,很难判断它是否泄漏。
TPessoa = class(TObject)
....
public
destructor Destroy; override;
end;
destructor TPessoa.Destroy;
begin
FContas.Free;
inherited;
end;
procedure SetContas(const Value: TContasCollection);
begin
FContas.Free; // release old FContas collection if there is one
FContas := Value; // reference FContas grabs ownership here
end;
基本上,您创建的每个对象在不再需要后都必须释放,除非有其他对象实例将获取该对象的所有权并为您执行此操作,例如 TObjectList
做。
以上代码假设FContas
字段是它所持有的集合的所有者,重要的是您不要获取该引用并将其保留到其所有者对象 ( TPessoa
) 实例的生命周期之外。
例如,以下代码将是错误的:
procedure TForm2.btn1Click(Sender: TObject);
var
pessoa : Tpessoa;
contas: TContasCollection;
begin
pessoa := ClientModule1.ServerMethods1Client.getPessoa(1);
contas := pessoa.Contas;
pessoa.Free; // <-- after that point contas points to released object
mmo1.Lines.Add(contas[0].Banco); // <-- dangling pointer use
end;
还有一些引用计数的对象实例,它们在最后一次引用超出范围后会自动释放(通常是TInterfacedObject
类的后代),但管理它们是完全不同的故事。您在这里没有使用它们,我提及它们只是为了完整性。
关于delphi - 如何通过DataSnap发送/接收TObjectList解决内存泄漏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29212614/