Autor Beitrag
Mortal-Shadow
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 110



BeitragVerfasst: Sa 13.09.08 15:32 
Hi,
In meinem Programm gibt es mehrere Forms.
In einer macht man Eingaben, und in der zweiten werden dann eine Anzahl Bilder abhängig von den Eingaben erschaffen.
Ca. so:
Dabei ist Bild ein dynamischer Array von TImage.
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
setlength(bild, Eingabe);
For i := 0 to (Eingabe -1do
   begin
   bild[i] := TImage.Create(nil);
   bild[i].parent := self;
   {...}

Wenn man nun die Bilderform schliest, wird die Eingabeform wieder gezeigt und man kann neue Angaben machen.
Die Frage ist nur - was mache ich mit den bisherigen Bildern?
Ich habe es bisher so versucht:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
procedure TfrmMain.FormClose(Sender: TObject; var Action: TCloseAction);
var count1: integer;
begin
Form1.show;
For count1 := (Eingabe - 1downto 0 do
    bild[count1].free;
end;

Allerdings bekomme ich bei mehrmaligem Ausführen die 'Accessviolation' - Fehlermeldung.
Die Bilder alle im Speicher liegen lassen bis Programmende will ich auch nicht, zumal man sie beim nächsten Aufruf der Bilderform noch sieht.
Wie könnte ich dieses Problem lösen?
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19343
Erhaltene Danke: 1752

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: So 14.09.08 13:50 
1. Wenn du als Parameter beim Erzeugen des TImage-Objekts nicht nil sondern die Form angibst, wird es aus dem Speicher entfernt, wenn das Formular aus dem Speicher entfernt wird, ein Freigeben ist dann nicht mehr nötig (und sollte auch nicht mehr geschehen).
2. Wenn du in FormClose Action auf caFree setzt, wird das Formular aus dem Speicher entfernt. Dann sind die Objekte auch ggf. alle freigegeben.
Du kannst das automatische Erzeugen der Formulare entweder ganz rausnehmen oder vorher prüfen, ob die Formular-Variable nil ist, bevor du vor der Anzeige des Formulars dieses erzeugst.
Hidden
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 2242
Erhaltene Danke: 55

Win10
VS Code, Delphi 2010 Prof.
BeitragVerfasst: So 14.09.08 14:12 
Und der Vollständigkeit halber: Deine Freigabe sollte besser so aussehen, dann würde das Problem afaik auch nicht auftreten:
ausblenden Delphi-Quelltext
1:
2:
For count1 := (Eingabe - 1downto 0 do
    FreeAndNil(bild[count1]);


mfG,

_________________
Centaur spears can block many spells, but no one tries to block if they see that the spell is a certain shade of green. For this purpose it is useful to know some green stunning hexes. (HPMoR)