退出时 释放全局类出错!(30分)

  • 主题发起人 主题发起人 liwowo
  • 开始时间 开始时间
L

liwowo

Unregistered / Unconfirmed
GUEST, unregistred user!
全局类结构

unit g_class_u;

interface

uses classes,SysUtils,Controls,mydata;

Type
Tg_class=class(TObject)
public
class function create_G(usrcode:string):Tg_class;
function findpwd(Ival:integer):boolean;
destructor Destroy
override;
private
mypwr:array of integer;
constructor Create(val:string);//输入权限组

end;

var g_class:Tg_class;

implementation



constructor Tg_class.Create(val: string);
var
Ssqlstr:string;
Inum:integer;
begin
inherited Create;
Ssqlstr:='select cFnNo from sysfunction where cFnno in (select CFnno from g_s where Gno='+val+') ';
with DataModule1.ADOQuery1 do
begin
close;
sql.clear;
sql.add(Ssqlstr);
open ;
if recordcount=0 then
begin
setlength(mypwr,1);
mypwr[0]:=0;
exit;
end;
setlength(mypwr,recordcount);
first;
for Inum:=0 to recordcount do
begin
mypwr[Inum]:=fieldbyname('cFnNo').asinteger;
next;
end;
close;
end;
end;

class function Tg_class.create_G(usrcode: string): Tg_class;
begin
if (g_class=nil) then
g_class:=Tg_class.create(usrcode);
result:=g_class;
end;

destructor Tg_class.Destroy;
begin

// setLength(myPwr,0);
// myPwr:=nil;
// inherited Destroy;
end;

function Tg_class.findpwd(Ival: integer): boolean;
var
Inum:integer;
begin

for Inum:=low(mypwr) to high(mypwr) do
begin
if mypwr[Inum]=Ival then
begin
result:=true;
exit;
end;
end;
result:=false;
end;

end.

然后在一个窗体中

procedure TMyForm1.FormShow(Sender: TObject);
begin
if g_class=nil then
g_class:=Tg_class.create_G(grpname);
end;

procedure TMyForm1.FormDestroy(Sender: TObject);
begin
if g_class<>nil then
FreeAndNil(g_class);
end;

怎么会出现错误呢! 各位帮着看看 我用是单例模式写的全局类 为什么我退出释放g_class的时候会出错!
 
帮您提吧我要吃饭了.
 
g_class:=Tg_class.create_G(nil);

這樣就ok.
 
是的,FORM DESTROY时grpname已经被析构,G_CLASS已被FREE一次,但是没有设置为NIL。
所以你再调用FreeAndNil会出错。
 

Similar threads

I
回复
0
查看
668
import
I
I
回复
0
查看
513
import
I
I
回复
0
查看
615
import
I
I
回复
0
查看
815
import
I
I
回复
0
查看
564
import
I
后退
顶部