博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Delphi 中的自动释放策略
阅读量:6826 次
发布时间:2019-06-26

本文共 5838 字,大约阅读时间需要 19 分钟。

  hot3.png

一、指定 Owner 后, 随 Owner 连带释放:

//uses Vcl.StdCtrls, Vcl.ExtCtrls;var  panel: TPanel;procedure TForm1.Button1Click(Sender: TObject);begin  panel := TPanel.Create(Self);  panel.Parent := Self;  with TButton.Create(panel) do //AOwner = panel  begin    Parent := panel;    Caption := 'New Button';  end;end;procedure TForm1.Button2Click(Sender: TObject);begin  if Assigned(panel) then    FreeAndNil(panel); //连带释放以它为 Owner 的对象end;

二、使用接口:

unit Unit1;interfaceuses  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;type  TForm1 = class(TForm)    Button1: TButton;    procedure Button1Click(Sender: TObject);  end;  //  IBass = Interface    function GetName: string;    procedure SetName(const AName: string);    property Name: string read GetName write SetName;  end;  //  TBass = class(TInterfacedObject, IBass)  private    FName: string;  protected    function GetName: string;    procedure SetName(const AName: string);  public    constructor Create(const AName: string);  end;var  Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);var  X: IBass;begin  X := TBass.Create('WanYi');  ShowMessage(X.Name);  X.Name := '万一';  ShowMessage(X.Name);  {X 在此自动释放}end;{ TBass }constructor TBass.Create(const AName: string);begin  FName := AName;end;function TBass.GetName: string;begin  Result := FName;end;procedure TBass.SetName(const AName: string);begin  FName := AName;end;end.

三、使用结构:

unit Unit1;interfaceuses  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;type  TForm1 = class(TForm)    Button1: TButton;    procedure Button1Click(Sender: TObject);  end;  //  TBass = record  private    FName: string;    procedure SetName(const AName: string);  public    constructor Create(const AName: string);    property Name: string read FName write SetName;  end;var  Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);var  X: TBass;begin  X := TBass.Create('WanYi');  ShowMessage(X.Name);  X.Name := '万一';  ShowMessage(X.Name);  {X 在此自动释放}end;{ TBass }constructor TBass.Create(const AName: string);begin  FName := AName;end;procedure TBass.SetName(const AName: string);begin  FName := AName;end;end.

四、在 initialization 中建立、在 finalization 中释放:

unit Unit1;interfaceuses  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;type  TForm1 = class(TForm)    Button1: TButton;    procedure Button1Click(Sender: TObject);  end;var  Form1: TForm1;implementation{$R *.dfm}var  List: TStringList;procedure TForm1.Button1Click(Sender: TObject);begin  List.Clear;  List.Add('WanYi');  ShowMessage(List.Text);end;initialization  List := TStringList.Create;finalization  List.Free;end.

五、使用动态数组而不是 TList、TStringList 等:

procedure TForm1.Button1Click(Sender: TObject);var  arr: Array of string;  i: Integer;  s: string;begin  for i := 0 to 6 do  begin    SetLength(arr, Length(arr)+1);    arr[High(arr)] := StringOfChar(Char(i+65), 3);  end;  for s in arr do ShowMessage(s);end;procedure TForm1.Button2Click(Sender: TObject);var  arr: TArray
; i: Integer; s: string;begin for i := 0 to 6 do begin SetLength(arr, Length(arr)+1); arr[High(arr)] := StringOfChar(Char(i+65), 3); end; for s in arr do ShowMessage(s);end;

六、使用 TObjectList 而不是 TList:

uses System.Contnrs;procedure TForm1.Button1Click(Sender: TObject);var  list: TObjectList;  i: Integer;  btn: TButton;begin  list := TObjectList.Create;  for i := 0 to 6 do  begin    btn := TButton.Create(Self);    with btn do begin      Caption := Format('Btn %d', [i+1]);      Parent := Self;      Top := Height * i;      Left := Width * i div 2;    end;    list.Add(btn);  end;  ShowMessage('TObjectList 释放时, 会同时释放其中的对象');  list.Free;end;procedure TForm1.Button2Click(Sender: TObject);var  list: TList;  i: Integer;  btn: TButton;begin  list := TList.Create;  for i := 0 to 6 do  begin    btn := TButton.Create(Self);    with btn do begin      Caption := Format('Btn %d', [i+1]);      Parent := Self;      Top := Height * i;      Left := Width * i div 2;    end;    list.Add(btn);  end;  ShowMessage('TList 释放后, 其中的对象并未释放');  list.Free;end;

七、使用 TObjectList<T> 而不是 TList<T>:

uses System.Generics.Collections;procedure TForm1.Button1Click(Sender: TObject);var  list: TObjectList
; i: Integer; btn: TButton;begin list := TObjectList
.Create; for i := 0 to 6 do begin btn := TButton.Create(Self); with btn do begin Caption := Format('Btn %d', [i+1]); Parent := Self; Top := Height * i; Left := Width * i div 2; end; list.Add(btn); end; ShowMessage('TObjectList 释放时, 会同时释放其中的对象'); list.Free;end;procedure TForm1.Button2Click(Sender: TObject);var list: TList
; i: Integer; btn: TButton;begin list := TList
.Create; for i := 0 to 6 do begin btn := TButton.Create(Self); with btn do begin Caption := Format('Btn %d', [i+1]); Parent := Self; Top := Height * i; Left := Width * i div 2; end; list.Add(btn); end; ShowMessage('TList 释放后, 其中的对象并未释放'); list.Free;end;

八、使用结构体而不是结构体指针:

{假如某个函数的参数需要一个结构指针}function Area(rect: PRect): Integer;begin  Result := rect.Width * rect.Height;//  Result := rect^.Width * rect^.Height;end;{直接声明指针并分配空间后需手动释放}procedure TForm1.Button1Click(Sender: TObject);var  P: PRect;begin  New(P);  P^ := Rect(10, 10, 60, 50);  ShowMessage(IntToStr(Area(P))); //2000  Dispose(P);end;{}procedure TForm1.Button2Click(Sender: TObject);var  R: TRect;begin  R := Rect(10, 10, 60, 50);  ShowMessage(IntToStr(Area(@R))); //2000end;

转载于:https://my.oschina.net/hermer/blog/319712

你可能感兴趣的文章
理解ClassLoader基本原理
查看>>
Java并发基础06. 线程范围内共享数据
查看>>
PHP实验三
查看>>
Python-序列号和模块复习-64
查看>>
查找目录下的所有文件中是否含有某个字符串
查看>>
12月6号,jQuery 序列化form
查看>>
aardio修改IDE里代码的字体
查看>>
FJNU2018低程F jq解救fuls (贪心乱搞)题解
查看>>
线程池原理及其实现
查看>>
HttpWebRequest发送数据 post
查看>>
MySQL的Root用户密码
查看>>
pip 使用
查看>>
_stdcall与_cdecl区别
查看>>
Ubuntu jsp平台使用JDBC来连接MySQL数据库
查看>>
Kinect For Windows V2开发日志四:使用OpenCV显示深度图像
查看>>
正则表达式 -- 常用元字符
查看>>
NDK获取android下sd card id
查看>>
解决 在窗口模式,点按钮防止弹出新页面
查看>>
linux 基础配置
查看>>
项目管理理论与实践(4)——UML应用(上)
查看>>