-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
method_with_self_nil.lpr
43 lines (35 loc) · 939 Bytes
/
method_with_self_nil.lpr
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
{$mode objfpc}{$H+}{$J-}
{ The expected output:
$ fpc method_with_self_nil.lpr
$ ./method_with_self_nil
WriteSomethig: Self <> nil? FALSE
Runtime error 216 at $000000000040119D
$000000000040119D
$0000000000422F7C
}
type
TMyClass = class
procedure WriteSomethig;
procedure WriteSomethigVirtual; virtual;
end;
procedure TMyClass.WriteSomethig;
begin
Writeln('WriteSomethig: Self <> nil? ', Self <> nil);
end;
procedure TMyClass.WriteSomethigVirtual;
begin
Writeln('WriteSomethigVirtual: Self <> nil? ', Self <> nil);
end;
var
C: TMyClass;
begin
{ This test deliberately calls method with Self = nil.
This is a dirty trick, should not be used (is definitely counter-intuitive as a programming pattern)
but it works on non-virtual methods,
and standard Free actually does it ("if Self <> nil then Destroy").
}
// this works
C.WriteSomethig;
// this will crash
C.WriteSomethigVirtual;
end.