客户端应用程序和 DataSnap 服务器的原型。我想将TObjectList从服务器传输到客户端。
这是可行的,但我传达的所有对象都保留在服务器和客户端的内存中。
我究竟做错了什么 ?
生命周期 = 会话
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 中的公共方法:
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 发生了意外的内存泄漏。意外的小块泄漏是:
1 - 12 字节:TMoveArrayManager x 1,未知 x 1 13 - 20 字节:TConta x 1,UnicodeString x 1 37 - 44 字节:TObjectList x 1
如何解决这种内存泄漏而不影响服务?