关于什么销毁对象的问题... ( 积分: 100 )

  • 主题发起人 主题发起人 jihaiming
  • 开始时间 开始时间
J

jihaiming

Unregistered / Unconfirmed
GUEST, unregistred user!
unit login;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Mask, ExtCtrls, DB, Grids, DBGrids, Contnrs,
DBClient, ComCtrls;

type
TForm1 = class(TForm)
edtName: TLabeledEdit;
edtPassword: TMaskEdit;
Label1: TLabel;
ClientDataSet1: TClientDataSet;
ClientDataSet1aotuID: TAutoIncField;
ClientDataSet1ID: TWideStringField;
ClientDataSet1NAME: TWideStringField;
ClientDataSet1SEX: TWideStringField;
ClientDataSet1JOB: TWideStringField;
ClientDataSet1TEL: TWideStringField;
ClientDataSet1CALL: TWideStringField;
ClientDataSet1DEP: TWideStringField;
ClientDataSet1GROUP_ID: TWideStringField;
ClientDataSet1PASSWORD: TWideStringField;
DataSource1: TDataSource;
btnLogin: TButton;
DBGrid1: TDBGrid;
btnHint: TButton;
StatusBar1: TStatusBar;
procedure FormCreate(Sender: TObject);
procedure btnLoginClick(Sender: TObject);
procedure btnHintClick(Sender: TObject);
private
ProfileList: TObjectList;
public
{ Public declarations }
end;

TProfile = class(TObject)
Name : String;
Dep : String;
Password :String;
Job : String;
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
i:integer;
AMan:TProfile;
begin
DBGrid1.Visible:=false;
self.Width:=310;
ClientDataSet1.LoadFromFile('login.dat');
ClientDataSet1.Active:=True;
ProfileList:=TObjectList.Create(True);
for i:=1 to ClientDataSet1.RecordCount do
begin
AMan:=TProfile.Create;
AMan.Name:=ClientDataSet1.FieldByName('Name').AsString;
AMan.Job:=ClientDataSet1.FieldByName('Job').AsString;
AMan.Dep:=ClientDataSet1.FieldByName('Dep').AsString;
AMan.Password :=ClientDataSet1.FieldByName('Password').AsString;
ProfileList.Add(AMan);
ClientDataSet1.Next;
end;
end;

procedure TForm1.btnLoginClick(Sender: TObject);
var
i:integer;
AMan:TProfile;
begin
for i :=0 to (ProfileList.Count-1) do
begin
AMan:=TProfile(ProfileList.Items);
if (Trim(AMan.Name)=Trim(edtName.Text)) and
(Trim(AMan.Password)=Trim(edtPassword.Text)) then
begin
application.MessageBox('登录成功。','提示',MB_OK+MB_ICONINFORMATION);
StatusBar1.Panels[0].Text:='当前用户:'+AMan.Name+AMan.Dep+AMan.Job;
exit;
end;
end;
application.MessageBox('非法用户,登录失败!','提示',MB_OK+MB_ICONSTOP);
end;

procedure TForm1.btnHintClick(Sender: TObject);
begin
if DBGrid1.Visible then
begin
self.Width:=310;
btnHint.Caption:='>>';
end
else
begin
self.Width:=713;
btnHint.Caption:='<<'
end;
DBGrid1.Visible:=not DBGrid1.Visible;
end;

end.

请问在上面的程序中,AMan对象创建后,是不是应该手动销毁呢? 好像它没有属主啊.
高手指点. 新人~
 
unit login;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Mask, ExtCtrls, DB, Grids, DBGrids, Contnrs,
DBClient, ComCtrls;

type
TForm1 = class(TForm)
edtName: TLabeledEdit;
edtPassword: TMaskEdit;
Label1: TLabel;
ClientDataSet1: TClientDataSet;
ClientDataSet1aotuID: TAutoIncField;
ClientDataSet1ID: TWideStringField;
ClientDataSet1NAME: TWideStringField;
ClientDataSet1SEX: TWideStringField;
ClientDataSet1JOB: TWideStringField;
ClientDataSet1TEL: TWideStringField;
ClientDataSet1CALL: TWideStringField;
ClientDataSet1DEP: TWideStringField;
ClientDataSet1GROUP_ID: TWideStringField;
ClientDataSet1PASSWORD: TWideStringField;
DataSource1: TDataSource;
btnLogin: TButton;
DBGrid1: TDBGrid;
btnHint: TButton;
StatusBar1: TStatusBar;
procedure FormCreate(Sender: TObject);
procedure btnLoginClick(Sender: TObject);
procedure btnHintClick(Sender: TObject);
private
ProfileList: TObjectList;
public
{ Public declarations }
end;

TProfile = class(TObject)
Name : String;
Dep : String;
Password :String;
Job : String;
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
i:integer;
AMan:TProfile;
begin
DBGrid1.Visible:=false;
self.Width:=310;
ClientDataSet1.LoadFromFile('login.dat');
ClientDataSet1.Active:=True;
ProfileList:=TObjectList.Create(True);
for i:=1 to ClientDataSet1.RecordCount do
begin
AMan:=TProfile.Create;
AMan.Name:=ClientDataSet1.FieldByName('Name').AsString;
AMan.Job:=ClientDataSet1.FieldByName('Job').AsString;
AMan.Dep:=ClientDataSet1.FieldByName('Dep').AsString;
AMan.Password :=ClientDataSet1.FieldByName('Password').AsString;
ProfileList.Add(AMan);
ClientDataSet1.Next;
end;
end;

procedure TForm1.btnLoginClick(Sender: TObject);
var
i:integer;
AMan:TProfile;
begin
for i :=0 to (ProfileList.Count-1) do
begin
AMan:=TProfile(ProfileList.Items);
if (Trim(AMan.Name)=Trim(edtName.Text)) and
(Trim(AMan.Password)=Trim(edtPassword.Text)) then
begin
application.MessageBox('登录成功。','提示',MB_OK+MB_ICONINFORMATION);
StatusBar1.Panels[0].Text:='当前用户:'+AMan.Name+AMan.Dep+AMan.Job;
exit;
end;
end;
application.MessageBox('非法用户,登录失败!','提示',MB_OK+MB_ICONSTOP);
end;

procedure TForm1.btnHintClick(Sender: TObject);
begin
if DBGrid1.Visible then
begin
self.Width:=310;
btnHint.Caption:='>>';
end
else
begin
self.Width:=713;
btnHint.Caption:='<<'
end;
DBGrid1.Visible:=not DBGrid1.Visible;
end;

end.

请问在上面的程序中,AMan对象创建后,是不是应该手动销毁呢? 好像它没有属主啊.
高手指点. 新人~
 
在Form.OnDestroy中将ProfileList对象Free掉即可,会自动把它包含List中的Object销毁掉的,看看它的源代码不就知道啦
 
利用
try
except
 
绝对要销毁的,如果你不嫌麻烦则逐个销毁Aman,最直接的就是销毁ProfileList。
 
但profilelist销毁了,可AMan对象呢.AMan是包含在List中的对象吗? 他不是邮profile类创建的吗?
 
ProfileList.Add(AMan)是Add指针嘛,所以可以全部销毁。
 
把profileList.OwnsObjects负值为True就可以了,你不用管了,ProfileList会自己释放的。
 
OwnsObjects property (TObjectList)
Allows TObjectList to free objects when they are deleted from the list or the list is destroyed.
Description

OwnsObjects allows TObjectList to control the memory of its objects. If OwnsObjects is true (the default),

calling Delete or Remove frees the deleted object in addition to removing it from the list.
calling Clear frees all the objects in the list in addition to emptying the list.
calling the destructor frees all the objects in the list in addition to destroying the TObjectList itself.
assigning a new value to an index in Items frees the object that previously occupied that position in the list.

Even if OwnsObjects is true, the Extract method can be used to remove objects from the list without freeing them.
 
哈哈,多谢各位高手指点.
小弟明白了. 就此散分. 顷家荡产.呵呵
多谢. 分少勿嫌.~
求各位高手的QQ~!
 
TObjectList

以前没注意过有这个。
以前一直都是用TList然后手动释放的。
 
后退
顶部