qt 判断指针对象是否释放
2个回答
展开全部
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TA = class
private
Fs: string;
public
constructor Create(s: string);
destructor Destroy;override;
procedure test;
end;
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
A: TA;
begin
A := TA.Create('test');
A.Free;
//A := nil;
//if A <> nil then
if Assigned(A) then
A.test;
end;
{ TA }
constructor TA.Create(s: string);
begin
Fs:= s;
end;
destructor TA.Destroy;
begin
Fs:= '';
inherited;
end;
procedure TA.test;
begin
ShowMessage(Fs);
end;
end.
例如以上代码,A对象所指向的堆内存已经free了,但无论判断A是否为nil或者用assigned判断,都可以调用A.test而不出错。应该怎么判断A已经Free而不至于执行A.test呢?
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TA = class
private
Fs: string;
public
constructor Create(s: string);
destructor Destroy;override;
procedure test;
end;
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
A: TA;
begin
A := TA.Create('test');
A.Free;
//A := nil;
//if A <> nil then
if Assigned(A) then
A.test;
end;
{ TA }
constructor TA.Create(s: string);
begin
Fs:= s;
end;
destructor TA.Destroy;
begin
Fs:= '';
inherited;
end;
procedure TA.test;
begin
ShowMessage(Fs);
end;
end.
例如以上代码,A对象所指向的堆内存已经free了,但无论判断A是否为nil或者用assigned判断,都可以调用A.test而不出错。应该怎么判断A已经Free而不至于执行A.test呢?
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询