我实现了以下类: type TUtilProcedure = procedure(var AJsonValue: TJSONObject); TCallback = class private FName: string; FProcedure: TUtilProcedure; FAnnotation: string; public constructor Create(AName: string; AProcedure: TUtilProce
type TUtilProcedure = procedure(var AJsonValue: TJSONObject); TCallback = class private FName: string; FProcedure: TUtilProcedure; FAnnotation: string; public constructor Create(AName: string; AProcedure: TUtilProcedure; AAnnotation: string); overload; constructor Create(ACallback: TCallback); overload; property Name: string read FName; property Proc: TUtilProcedure read FProcedure; property Annotation: string read FAnnotation; end;
然后我有一个全局变量:
procedures: TDictionary<string, TCallback>;
在OnFormActivate过程中我初始化过程变量:
procedures := TDictionary<string, TCallback>.Create(); procedures.Add('something', TCallback.Create('sth', @proc, 'annotation')); // ....
然后在OnFormClose中我释放它:
procedures.Clear; procedures.Free;
我的代码是否泄漏内存?如果是这样,解释字典的正确方法是什么?
据我所知,迭代不是好主意.
如果您需要将对象存储在字典中,则采用TObjectDictionary表示更好的方法.
如果希望自动释放字典中包含的对象,请在创建集合实例时使用doOwnsValues标志.
>当变量实际上是全局变量(即在单元的接口部分中声明为var)时,应在单元本身的初始化和完成部分中创建和销毁变量.
. . . var procedures: TObjectDictionary<string, TCallback>; . . . initialization procedures:= TObjectDictionary<string, TCallback>.Create([doOwnsValues]); finalization procedures.Free;
>当您的变量属于表单类本身时,您应该在表单的OnCreate事件中创建字典.
. . . public procedures: TObjectDictionary<string, TCallback>; . . . procedure TForm1.FormCreate(Sender: TObject); begin procedures:= TObjectDictionary<string, TCallback>.Create([doOwnsValues]); end;
在表单的OnDestroy事件中释放字典:
procedure TForm1.FormDestroy(Sender: TObject); begin procedures.Free; end;
>此外,如果您希望访问属于该类的变量而无需类本身的实例(这在许多编程语言中称为静态变量),您可以将字典声明为类var并可选通过类属性访问它;在这种情况下,最好在类构造函数和类析构函数中创建和销毁集合.
. . . TMyClass = class private class constructor Create; class destructor Destoy; public class var procedures: TObjectDictionary<string, TCallback>; end; . . . class constructor TMyClass.Create; begin procedures := TObjectDictionary<string, TCallback>.Create([doOwnsValues]); end; class destructor TMyClass.Destoy; begin procedures.Free; end;
TCallback = class private FName: string; FProcedure: TUtilProcedure; FAnnotation: string; public constructor Create(AName: string; AProcedure: TUtilProcedure; AAnnotation: string); overload; constructor Create(ACallback: TCallback); overload; property Name: string read FName; property Proc: TUtilProcedure read FProcedure; property Annotation: string read FAnnotation; end;
作为旁注,TCallback类不需要指定析构函数,因为它只拥有两个字符串和一个指向过程的指针.因此从TObject继承的默认析构函数就足够了.