Autor Beitrag
wazup
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 101

Windows XP
Delphi 2009
BeitragVerfasst: Fr 15.08.08 17:31 
HI

Mich würde es mal Interessieren ob man das Objekt hiernach freigeben muss oder ob es Automatisch gemacht wird:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
begin
  with TIdHTTP.Create(NIL) do
  begin
    //...
  end;
end;


Gruß
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8554
Erhaltene Danke: 481

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Fr 15.08.08 17:51 
Du hast es erstellt, also musst du es freigeben.

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
begin 
  with TIdHTTP.Create(NIL) do 
  begin 
    //... 
    free;
  end; 
end;


Oder du gibst als Owner die Form an, wenn der Code innerhalb einer Form steht.

_________________
We are, we were and will not be.
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Fr 15.08.08 17:56 
Moin!

Zur Erläuterung:
user profile iconGausi hat folgendes geschrieben:
Du hast es erstellt, also musst du es freigeben.
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
begin 
  with TIdHTTP.Create(NIL) do // und zwar deshalb!
  begin 
    //... 
    free;
  end; 
end;
In diesem Fall bist du der Eigentümer, also musst du dich auch um´s Aufräumen kümmern. :idea:

user profile iconGausi hat folgendes geschrieben:
Oder du gibts als Owner die Form an, wenn der Code innerhalb einer Form steht.
Dann darfst du es nicht selbst freigeben, es gehört ja nicht (mehr) dir! ;)

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
wazup Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 101

Windows XP
Delphi 2009
BeitragVerfasst: Fr 15.08.08 23:06 
Also... Nochmal im klar text:

-> Wenn ich einen Owner angebe darf ich es nicht freigeben
-> Wenn ich keinen angebe muss ich es freigeben
Motzi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2931

XP Prof, Vista Business
D6, D2k5-D2k7 je Prof
BeitragVerfasst: Sa 16.08.08 13:39 
user profile iconwazup hat folgendes geschrieben:
Also... Nochmal im klar text:

-> Wenn ich einen Owner angebe darf ich es nicht freigeben
-> Wenn ich keinen angebe muss ich es freigeben

Nicht ganz.. wenn du einen Owner angibst kannst du es freigeben, musst aber nicht. Wenn du es nicht tust wird es automatisch freigegeben sobald der Owner zerstört wird. Wenn du keinen Owner angibst bist du selbst dafür verantwortlich, dass das Objekt freigegeben wird. Im übrigen solltest du einen try-finally-Block verwenden:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
begin 
  with TIdHTTP.Create(nil) do 
  try
    //... 
  finally
    Free;
  end; 
end;


Gruß, Motzi

_________________
gringo pussy cats - eef i see you i will pull your tail out by eets roots!
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Sa 16.08.08 17:53 
Moin!

user profile iconMotzi hat folgendes geschrieben:
wenn du einen Owner angibst kannst du es freigeben, musst aber nicht.
Und wie kommt dann die Referenz aus dem Controls-Array darauf, dass das Objekt schon freigegeben ist? :nixweiss:

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Motzi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2931

XP Prof, Vista Business
D6, D2k5-D2k7 je Prof
BeitragVerfasst: Sa 16.08.08 18:06 
Aus der Classes.pas:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
destructor TComponent.Destroy;
begin
  Destroying;
  if FFreeNotifies <> nil then
  begin
    while Assigned(FFreeNotifies) and (FFreeNotifies.Count > 0) do
      TComponent(FFreeNotifies[FFreeNotifies.Count - 1]).Notification(Self, opRemove);
    FreeAndNil(FFreeNotifies);
  end;
  DestroyComponents;
  if FOwner <> nil then FOwner.RemoveComponent(Self);
  inherited Destroy;
end;

Die VCL sorgt intern automatisch dafür. Wenn bei einer TComponent-Instanz der Owner gesetzt ist, dann entfernt sie sich selbst aus dessen Liste.

Gruß, Motzi

_________________
gringo pussy cats - eef i see you i will pull your tail out by eets roots!


Zuletzt bearbeitet von Motzi am So 17.08.08 15:06, insgesamt 1-mal bearbeitet
Tilman
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1405
Erhaltene Danke: 51

Win 7, Android
Turbo Delphi, Eclipse
BeitragVerfasst: Sa 16.08.08 23:48 
Ich mache das immer wenn ich Objekte zur Laufzeitverzeuge: ich stelle als Owner self ein. Und trotzdem gebe ich sie gelegentlich selber wieder frei - bisher ohne jedes Problem. Wusste auch gar nicht das Create(nil) legitim / empfehlenswert ist.

_________________
Bringe einen Menschen zum grübeln, dann kannst du heimlich seinen Reis essen.
(Koreanisches Sprichwort)