我有以下代碼序列:在Delphi獲取的對象的分配地址7
program OverrideAfterConstructionEtc;
{$APPTYPE CONSOLE}
uses
SysUtils, Classes;
type
TA = class(TInterfacedObject)
public
procedure AfterConstruction; override;
procedure BeforeDestruction; override;
protected
FDummyData: array[ 1 .. 1000 ] of longint;
end;
{ TA }
procedure TA.AfterConstruction;
var
selfPtr: Pointer;
selfInt: Integer;
selfStr: string;
size: Integer;
begin
inherited AfterConstruction;
selfPtr := Addr(self);
selfInt := Integer(selfPtr);
selfStr := IntToHex(selfInt, 8);
size := TA.InstanceSize;
WriteLn('TA instance allocated at 0x', selfStr);
WriteLn('TA size is ', size);
end;
procedure TA.BeforeDestruction;
var
selfPtr: Pointer;
selfInt: Integer;
selfStr: string;
size: Integer;
begin
selfPtr := Addr(self);
selfInt := Integer(selfPtr);
selfStr := IntToHex(selfInt, 8);
WriteLn('Preparing to destroy TA instance allocated at 0x', selfStr);
size := TA.InstanceSize;
WriteLn('TA size is ', size);
inherited BeforeDestruction;
end;
const
maxDummy = 1000;
var
a: TA;
dummy: TList;
iter : integer;
dummies: array [ 1 .. maxDummy ] of TList;
begin
// Simulate a set of allocations.
for iter := 1 to maxDummy do
begin
dummy := TList.Create;
dummies[ iter ] := dummy;
end;
// Allocate the object we want to track.
a := TA.Create;
// Release the simulated allocations.
for iter := 1 to maxDummy do
begin
dummy := dummies[ iter ];
dummies[ iter ] := nil;
FreeAndNil(dummy);
end;
// Release the tracked object.
FreeAndNil(a);
end.
代碼的輸出:
-
在0x0012FF88分配
- TA實例
- TA大小是4012準備銷燬
- 在0x0012FF80處分配的TA實例
- TA大小爲4012
我不明白「自我」的差異。你能給我一個提示嗎?我本來期望的印刷值是相同的。
謝謝你的回答。我如何將它們都標記爲答案(Web UI只允許我選擇其中之一)? – Dan 2011-01-13 07:23:43