【发布时间】:2013-12-08 12:50:10
【问题描述】:
我有一个返回IInterface 的方法,该方法包含一个这样创建的字典:
Dictionary:= TDictionary<string, IInterface>.Create;
包含:
Dictionary.Add('key', TPerson.Create(DatabaseCon)as IInterface);
当我退出正在进行的程序时,我收到一条错误消息内存泄漏对象 Tperson。
我在类析构函数中尝试了以下方法:
private //Global variabels
Person:TPerson;
I: IInterface;
begin // in the destructor method of the class
Person:= IInterface as TPerson;
Person.Free;
end;
但我仍然收到错误消息...如何释放TPerson.Create(DatabaseCon) as IInterface)?
或者将TPerson保存为INI文件中的字符串,然后将字符串名称拖出并变成对象会更好。但是如何??
程序的结构:
Inf1= Interface(IInterface)
//some functions
Inf2=Interface(Inf1)
//Some functions
TPerson= class(TInterfacedPersistent,Inf2)
// All my functions.
如果我满足以下条件,我可以让它工作:
Dictionary.Add ('key', Person) as IInterface);
我在类的 Create 方法中写:
Person:= TPerson.Create(DatabaseCon);
并在析构方法中写道:
Person.Free;
但是这样做有错吗?因为当我访问字典时必须首先实例化班级人员?
【问题讨论】:
标签: delphi delphi-xe4