Autor Beitrag
Danny87
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 688

Windows 10 Pro 64bit
Sprachen: HTML, PHP, JavaScript, Delphi || IDE: RAD Studio 10.1 Berlin Starter, WeBuilder
BeitragVerfasst: Mi 15.02.06 11:54 
Moin,
ich hab mal ne ganz bescheuerte Frage ;-)

Macht
ausblenden Delphi-Quelltext
1:
FreeAndNil(Form2);					

das gleiche wie
ausblenden Delphi-Quelltext
1:
2:
Form2.Free;
Form2 := nil;
?


gruss daniel
Vera
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 82

WinXP Home
Delphi 2005 Personal
BeitragVerfasst: Mi 15.02.06 11:58 
Ja, so ungefähr. ;-)

So sieht die FeeAndNil Implementation aus:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
procedure FreeAndNil(var Obj);
var
  Temp: TObject;
begin
  Temp := TObject(Obj);
  Pointer(Obj) := nil;
  Temp.Free;
end;


Vera
Danny87 Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 688

Windows 10 Pro 64bit
Sprachen: HTML, PHP, JavaScript, Delphi || IDE: RAD Studio 10.1 Berlin Starter, WeBuilder
BeitragVerfasst: Mi 15.02.06 12:01 
kann ich also FreeAndNil anstatt der anderen version benutzen oder sollte ich das lieber nicht?
AXMD
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 4006
Erhaltene Danke: 7

Windows 10 64 bit
C# (Visual Studio 2019 Express)
BeitragVerfasst: Mi 15.02.06 12:08 
Warum nicht was nehmen, was schon da ist ;)? FreeAndNil funktioniert super - und SysUtils musst du ohnehin in fast jedes Projekt einbinden.

AXMD
Alpha_Wolf
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 297

Ubuntu, Win XP, Win Vista
C#, Delphi 6 Prof, Delphi 2007 Prof, Java
BeitragVerfasst: Mi 15.02.06 12:10 
Natürlich kannst du FreeAndNil benutzen. Im endeffekt ist es das gleiche. Aber wieso jedes mal aufs neue erst Free und dann Nil (gibts doch schon in SysUtils)aufrufen wenn es das ganze sowieso schon gibt?

Ich persönlich nutze sogut wie immer FreeAndNil wenns nötig ist.


EDIT: too late ;)

_________________
Diskutiere nie mit einem Irren - er zieht dich auf sein Niveau und schlägt dich mit seiner Erfahrung.
Danny87 Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 688

Windows 10 Pro 64bit
Sprachen: HTML, PHP, JavaScript, Delphi || IDE: RAD Studio 10.1 Berlin Starter, WeBuilder
BeitragVerfasst: Mi 15.02.06 12:13 
Dankeschön!
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Do 16.02.06 12:59 
Ich für meinen Fall nutze auch immer FreeAndNil, bzw. bei Omorphia eine Abwandlung dieser Funktion, die wenn nötig (Object nicht Assigned) eine Debugmeldung auf's Debug-System legt. Ansonsten sollte man aber immer FreeAndNil bevorzugt verwenden, um Vergesslichkeit zu vermeiden *g*

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
digi_c
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1905

W98, XP
D7 PE, Lazarus, WinAVR
BeitragVerfasst: Do 16.02.06 13:56 
Wieso sollte aber ein Objekt nach dem .Free nicht automatisch NILsein? So ists doch gedacht und bei ir hat das immer geklappt.
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Do 16.02.06 14:05 
Das hast Du falsch verstanden: Die erweiterte Funktion von mir gibt eine Debug-Meldung aus, wenn man

ausblenden Delphi-Quelltext
1:
2:
3:
var Foo: TObject;
Foo := Nil;
FreeAndNil(Foo);


hat, was zwar kein Fehler ist, aber auf Probleme im Source hinweist, da man normalerweise mit Referenzen auf Objekte nur solange zugreifen sollte, wie diese auch initialisiert sind. Gibt man also ein Object frei, was bereits NIL ist, ist es höchstwahrscheinlich, dass ein Fehlverhalten aufgetreten ist (was wenn's nicht vorher ne AV gab) spätestens hier eine Warnung hervorrufen sollte.

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.