Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program Project1;
- {$APPTYPE CONSOLE}
- uses SysUtils, Classes;
- var
- sl : TStringList;
- begin
- sl := TStringList.Create;
- sl.Free;
- if Assigned(sl) then
- WriteLn('sl is still assigned')
- else
- WriteLn('sl is not assigned');
- end.
- procedure FreeAndNil(var Obj);
- var
- P: TObject;
- begin
- P := TObject(Obj);
- TObject(Obj) := nil; // clear the reference before destroying the object
- P.Free;
- end;
- procedure TObject.Free;
- asm
- TEST EAX,EAX
- JE @@exit
- MOV ECX,[EAX]
- MOV DL,1
- CALL dword ptr [ECX].vmtDestroy
- @@exit:
- end;
- var
- sl1, sl2: TStringList;
- begin
- sl1 := TStringList.Create;
- sl2 := sl1;
- FreeAndNil(sl1);
- // sl2 is still assigned and must be cleared separately (not with FreeAndNil because it points to the already freed object.)
- end;
- procedure TObject.Free;
- asm
- TEST EAX,EAX
- JE @@exit // Jump to exit if pointer is nil.
- MOV ECX,[EAX]
- MOV DL,1
- CALL dword ptr [ECX].vmtDestroy // Call cleanup code (and destructor).
- @@exit:
- end;
- if Obj != NIL then
- vmtDestroy(obj); // which is basically the destructor/deallocator.
Add Comment
Please, Sign In to add comment