Autor Beitrag
Born-to-Frag
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1094

Win XP SP2, Win 2000 SP4
Delphi 7, 2k5
BeitragVerfasst: Fr 18.08.06 01:44 
Hallo!

Ich erstelle eine Form zur laufzeit und gebe die unter Umständen bei OnClose mit Action := caFree wieder frei.. nur wenn ich mit Assigned() prüfen will ob die Form erstellt ist, ergibt dies immer True (außer vor dem ersten erstellen)

Weiß jemand warum?


greetz

_________________
Theorie ist wenn man alles weiß, aber nichts funktioniert. Praxis ist wenn alles funktioniert, aber niemand weiß warum.
Microsoft vereint Theorie und Praxis: Nichts funktioniert und niemand weiß warum.
uwewo
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 154



BeitragVerfasst: Fr 18.08.06 05:50 
Hi,

so könntest Du prüfen ob Form erstellt wurde und ansonsten neu erstellen.

ausblenden Delphi-Quelltext
1:
  if Form = nil then Form := TForm.Create(Application);					
Raffo
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 300



BeitragVerfasst: Fr 18.08.06 06:57 
Das Problem ist, das es dennoch im Speicher bleibt (für einige Zeit). Wenn man versucht:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
application.createform(tform2,form2);
form2.showmodal;
form2.free;

if form2=nil then caption:='aus' else caption:='ein';


bekommt man unten immer "ein".

Ich habe mir für dieses Prob mit einer Hilfvariablen geholfen, auf die global zugegriffen werden kann, z.B. Form2_isCreated:=boolean, die man dann in OnCreate und OnClose setzt.
rd3
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Fr 18.08.06 08:11 
Du kannst das auch so prüfen:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
procedure ...
var
  aComponent: TComponent;
begin
  aComponent := Application.FindComponent('aForm');
  if not Assigned (aComponent) then aForm := TaForm.Create(Application);
  // ... Anweisungen, wie aForm.Show; oder so...
end;
Martok
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 3661
Erhaltene Danke: 604

Win 8.1, Win 10 x64
Pascal: Lazarus Snapshot, Delphi 7,2007; PHP, JS: WebStorm
BeitragVerfasst: Fr 18.08.06 11:59 
Wenn man eine Variable auf nil Prüft, heißt das nur ob sie irgendwohin zeigt. Nach dem Free zeigt sie immer noch auf due alte Adresse, nur dass da jetzt nichts mehr steht. Daher muss man selbst dafür sorgen, dass die Variable auf nil gesetzt wird: form2:= nil;
oder, wenn du es kürzer willst:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
try
  application.createform(tform2,form2);
  form2.showmodal;
finally
  FreeAndNil(form2);
end;

_________________
"The phoenix's price isn't inevitable. It's not part of some deep balance built into the universe. It's just the parts of the game where you haven't figured out yet how to cheat."
alias5000
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2145

WinXP Prof SP2, Ubuntu 9.04
C/C++(Code::Blocks, VS.NET),A51(Keil),Object Pascal(D2005PE, Turbo Delphi Explorer) C# (VS 2008 Express)
BeitragVerfasst: Fr 18.08.06 13:12 
Aber das funktioniert IMHO auch:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
TForm2.FomClose(Action: TCloseAction);
begin
  Action := caFree;
  Self := nil
end;


Ich meine das mal erfolgreich eingesetzt zu haben *grübel*

Gruß alias5000

_________________
Programmers never die, they just GOSUB without RETURN
Born-to-Frag Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1094

Win XP SP2, Win 2000 SP4
Delphi 7, 2k5
BeitragVerfasst: Fr 18.08.06 13:58 
@Alias: So sachen hab' ich alles schon versucht =/ auch einfach FreeAndNil(Self).. hilft alles nix.. an try..except hab ich auch gedacht aber das wäre doch bisschen unprofessionell :(

ich zeige die Form ja auch nicht mit ShowModal sondern einfach Show an..


greetz

_________________
Theorie ist wenn man alles weiß, aber nichts funktioniert. Praxis ist wenn alles funktioniert, aber niemand weiß warum.
Microsoft vereint Theorie und Praxis: Nichts funktioniert und niemand weiß warum.
AndyB
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1173
Erhaltene Danke: 14


RAD Studio XE2
BeitragVerfasst: Fr 18.08.06 17:20 
user profile iconalias5000 hat folgendes geschrieben:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
TForm2.FomClose(Action: TCloseAction);
begin
  Action := caFree;
  Self := nil
end;


Ich meine das mal erfolgreich eingesetzt zu haben *grübel*

Na hoffentlich findet das Programm, in dem du das "erfolgreich eingesetzt" hast, nicht den Weg zu mir. Das funktioniert nicht. Was bringt es, wenn ich EAX (=Self) auf 0 setze, wenn die Variable auf dem Stack/Datensegment immernoch auf das (nicht mehr vorhandene) Objekt verweist.
Und um das noch mehr zu verdeutlichen:
Man hat drei Variablen f1, f2 und f3. Alle drei referenzieren dasselbe Formular. Wenn man nun "Self := nil" schreibt, welche der Variable sollte auf nil gesetzt werden? Vor allem wenn das Objekte gar nicht weiß von welcher aus es aufgerufen wurde und eigentlich gar keine Ahnung hat wo die Variablen im Speicher liegen, weil es nur den Wert ( = den Zeiger auf das Objekt) in EAX mitbekommen hat.

Zitat:
So sachen hab' ich alles schon versucht =/ auch einfach FreeAndNil(Self).. hilft alles nix..

Wenn du nur Show statt ShowModal benutzt, dann ist der Zeitpunkt an dem du deine globale Variable auf NIL setzen solltest OnDestroy.

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
procedure TForm1.FormDestroy(...)
begin
  Form1 := nil;
end;

class procedure TForm1.ShowForm;
begin
  if not Assigned(Form1) then
    Form1 := TForm1.Create(Application);
  Form1.Show;
end;

_________________
Ist Zeit wirklich Geld?
Born-to-Frag Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1094

Win XP SP2, Win 2000 SP4
Delphi 7, 2k5
BeitragVerfasst: Fr 18.08.06 18:56 
Hallo!

Also im OnDestroy Self := nil hat auch nichts gebracht, deswegen hab ich es jetzt mit einer Boolean-Variable und über NotifyEvents gelöst :)


greetz


EDIT: Nein, ich setze die Variable jetzt einfach in meinem Event auf nil, das funktioniert gut und ich brauche keine weiteren Variaben :D

_________________
Theorie ist wenn man alles weiß, aber nichts funktioniert. Praxis ist wenn alles funktioniert, aber niemand weiß warum.
Microsoft vereint Theorie und Praxis: Nichts funktioniert und niemand weiß warum.
AndyB
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1173
Erhaltene Danke: 14


RAD Studio XE2
BeitragVerfasst: Fr 18.08.06 19:07 
Das das in OnDestroy mit CloseAction := caFree nicht funktioniert, liegt wohl daran, dass das Formular noch nicht freigeben ist, aber nicht mehr sichtbar ist. CloseAction := caFree ruft nämlich TForm.Release auf, was dazu führt, dass das Formular erst beim nächsten Application.ProcessMessages oder bei der Anwendunges-Botschaftsschleife freigegeben wird.

_________________
Ist Zeit wirklich Geld?