Board index » delphi » Object Still Assigned After Destruction
|
Tony.DeMarsh
Delphi Developer |
Object Still Assigned After Destruction2008-07-29 01:24:17 AM delphi58 I'm having trouble understanding why the Item1 object isn't being set to nil in the following example. After the Transaction.Free; line is executed, the debugger shows that Item1's value is still set to ($20) or sometimes ($C), but before this line, it is something like ($D53DC8). Shouldn't the ItemList := nil; line in the TTransaction.Destroy destructor be setting the Item1's value to nil as well? unit Main; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TItem1 = class Info : TList; constructor Create; destructor Destroy; override; end; TTransaction = class ItemList : TList; constructor Create; destructor Destroy; override; end; TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} constructor TTransaction.Create; begin inherited; ItemList := TList.Create; end; destructor TTransaction.Destroy; var i : Integer; begin for i := 0 to ItemList.Count-1 do begin TObject(ItemList[i]).Free; ItemList[i] := nil; end; ItemList.Free; ItemList := nil; inherited; end; constructor TItem1.Create; begin inherited; Info := TList.Create; end; destructor TItem1.Destroy; var i : Integer; begin for i := 0 to Info.Count-1 do begin TObject(Info[i]).Free; Info[i] := nil; end; Info.Free; inherited; end; procedure TForm1.Button1Click(Sender: TObject); var Transaction : TTransaction; Obj : TObject; Item1 : TItem1; begin Transaction := TTransaction.Create; Item1 := TItem1.Create; Obj := TObject.Create; Item1.Info.Add(Obj); Transaction.ItemList.Add(Item1); Transaction.Free; if Assigned(Item1) then ShowMessage('hi'); end; end. |
