★★资源释放的问题!~请经验极其丰富的富翁观看或解答!(200分)

  • 主题发起人 主题发起人 kaithink
  • 开始时间 开始时间
K

kaithink

Unregistered / Unconfirmed
GUEST, unregistred user!
请高手指点!
-------------------------------------------------
program testprj;

uses
Controls,
Forms,
MainFormUnit in '../testprj/MainFormUnit.pas' {MainForm},
Kernel in '../testprj/Kernel.pas',
LoginFormUnit in '../testprj/LoginFormUnit.pas' {LoginForm};

{$R *.RES}

begin
Application.Initialize;
Application.CreateForm(TMainForm, MainForm);
LoginForm := TLoginForm.Create(Application);
try
if LoginForm.ShowModal = mrOk then
begin
Application.ShowMainForm := True; //走这边,再退出应用无异常.
Application.Run;
end else
begin
Application.ShowMainForm := False;
Application.Terminate; //A走这边,退出时抛出一个地址访问异常.
end;
finally
LoginForm.Free; //A异常就出现在此句之后,这就是我的恶梦!
end;
end.
------------------------------------------------
unit MainFormUnit;

interface

uses
..........
......
implementation
{$R *.DFM}

procedure TMainForm.FormCreate(Sender: TObject);
begin
FFunctionList := TList.Create;
FCurrentFunctionList := TList.Create;
Caption := Application.Title;
end;
procedure TMainForm.FormDestroy(Sender: TObject);
begin
if FCurrentFunctionlist<>nil then
begin
FCurrentFunctionList.Free;
FCurrentFunctionList:=nil;
end;
if FFunctionList<>nil then
begin
FFunctionlist.Free;
FFunctionlist:=nil;
end;
end;
----------------------------------------

转移贴子到 Visual C++ Visual Basic Delphi C/C++ C++ Builder ASM 数据库开发 PowerBuilder ActiveX/COM 数据算法 软件工程 图形/游戏/多媒体 安全技术 网络/通讯 技术书籍 文件格式 C# XML/SOAP 数据仓库 VBA 主页制作 图形处理 ASP PHP Perl Java Javascript JSP 嵌入式开发 .NET Framework lotus开发使用 ADO.NET ASP.NET Web Services Web Forms Windows Forms VB.NET VC.NET Windows 9x/ME Windows NT/2000/XP DOS Office使用 Office开发 Exchange server IIS 程序人生 软件市场 程序员考试 人才热线 建议和意见 灌水乐园 走向海外 程序员杂志 软件使用 资源使用 我看软件 寻找软件 行业软件 软件评测 Linux/Unix 硬件指南 程序员大本营 | 贴子加分 | 密码:


回复贴子:
 zswang(伴水)(被黑中) 回复于2001-12-13 16:05:48
begin
with TLoginForm.Create(nil) do try //建议
if ShowModal <> mrOk then Exit;
finally
Free;
end;
Application.Initialize;
Application.CreateForm(TMainForm, MainForm);
Application.Run;
end.

 zswang(伴水)(被黑中) 回复于2001-12-13 16:07:34
begin
Application.Initialize;
Application.CreateForm(TMainForm, MainForm);
LoginForm := TLoginForm.Create(Application); //Application.Free会触发LoginForm.Free
try
if LoginForm.ShowModal = mrOk then
begin
Application.ShowMainForm := True;
Application.Run;
end else
begin
Application.ShowMainForm := False;
Application.Terminate;
end;
finally
if Assigned(LoginForm) then LoginForm.Free;
end;
end.

 kaithink(凯森克) 回复于2001-12-13 16:14:11
感谢!但是仍然不行!是否我的MainForm的问题?

LoginForm := TLoginForm.Create(nil);这样也不行.
 lovewangj(lllll) 回复于2001-12-13 16:17:59
建议将try 。。。finally 去掉,你用if就已经进行错误处理了,你使用了
Application.Terminate后就不要LoginFm.Free了,因为Application.Terminate已经替你作过了,所以才会出错的。
 ihihonline(小小) 回复于2001-12-13 16:18:22
哎,再过几天提这个问题的话,我肯定能知道,今天只有看的份了

 kaithink(凯森克) 回复于2001-12-13 16:24:32
begin
Application.Initialize;
Application.CreateForm(TMainForm, MainForm);
LoginForm := TLoginForm.Create(nil); //nil or Application均一样出错
if LoginForm.ShowModal = mrOk then
begin
MainForm.UserCode := LoginForm.CodeEdit.Text;
SetCurrentUserCode(LoginForm.CodeEdit.Text);
Application.ShowMainForm := True;
Application.Run;
end else
begin
Application.ShowMainForm := False;
Application.Terminate;
end;
end.

 
我做了这样的测试,操作系统是windows2000,用的是delphi5
结果是没有异常错误发生,请贴出其它的代码,比如你的mrok按钮中
是否有代码,mrcancel中是否有代码?
program Project1;

uses
Controls,
Forms,
Unit1 in 'Unit1.pas' {Form1},
Unit2 in 'Unit2.pas' {Form2};

{$R *.RES}

begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Form2:=TForm2.Create(nil);
try
if Form2.ShowModal=mrOk then
begin
Application.ShowMainForm:=True;
Application.Run;
end
else
begin
Application.ShowMainForm := False;
Application.Terminate; //A走这边,退出时抛出一个地址访问异常.
end;
finally
form2.Free;
end;
end.
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
FFunctionList: TList;
FCurrentFunctionList:TList;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
FFunctionList := TList.Create;
FCurrentFunctionList := TList.Create;
Caption := Application.Title;

end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
if FCurrentFunctionlist<>nil then
begin
FCurrentFunctionList.Free;
FCurrentFunctionList:=nil;
end;
if FFunctionList<>nil then
begin
FFunctionlist.Free;
FFunctionlist:=nil;
end;

end;

end.
 
主程序已OK!正确写法列下:
-----------------------------------
{$R *.RES}

begin
Application.Initialize;
LoginForm := TLoginForm.Create(Application);
try
if LoginForm.ShowModal = mrOk then
begin
Application.CreateForm(TMainForm, MainForm);
Application.ShowMainForm := True;
Application.Run;
end else
begin
Application.ShowMainForm := False;
Application.Terminate;
end;
finally
if Assigned(LoginForm) then LoginForm.Free;
end;
end.
----------------------------------------------
主窗体看来有点毛病,导致主程序出错!
procedure TMainForm.FormDestroy(Sender: TObject);
begin
if Assigned(FCurrentFunctionlist) then
begin
FCurrentFunctionList.Free;
FCurrentFunctionList:=nil;
end;
if Assigned(FFunctionList) then
begin
// FFunctionlist.Free; //就是这句!看来该列表早已释放!
FFunctionlist:=nil;
end;
if Assigned(Fmenu) then
begin
FMenu.Free;
Fmenu:=nil;
end;
end;

procedure TMainForm.FormShow(Sender: TObject);
begin
..........
// 声明:这是我从别人手上接过来的活儿!
FCurrentFunctionList.Free ;
FCurrentFunctionList := FFunctionList;
//看这个变态的写法!
................
end;
 
接受答案了.
 
后退
顶部