Autor Beitrag
delphisual
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 89

WIN 2000
D5 Prof
BeitragVerfasst: Sa 13.09.03 14:17 
Hallo Leute,

ich habe ein kleines Problem: Ich habe eine Array mit 100 Elementen die fülle ich mit 100 Buttons, beim Versuch die Buttons wieder aus dem Speicher zu löschen erschein ein Gehler mit Zugriffsverletzung...

Kann jemand erkennen wo mein Fehler liegt?

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    free: TButton;
    procedure FormCreate(Sender: TObject);
    procedure freeClick(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;
  myArray: Array[0..99of TButton;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
  i: integer;
begin
  for i:=0 to 99 do begin
  with myArray[i] do begin
    with TButton.Create(self) do begin
       Parent:=Form1;
       Left:= 30;
       Top:= 30*i;
       Caption:='Butten'+IntToStr(i);
                                 end;
                     end;
                    end;
end;

procedure TForm1.freeClick(Sender: TObject);
var
  i: integer;
begin
  for i:=0 to 99 do begin
  with myArray[i] do begin
    Visible:= false;  // <-- Zugrifsverletzung, aber warum?
    free;             // <-- Zugrifsverletzung, aber warum?
    
                     end;
                    end;
end;

end.


danke schön

Moderiert von user profile iconTino: Code- durch Delphi-Tags ersetzt.

_________________
(Wer ein Problem definiert, hat es schon halb gelöst.)
cbs
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 207
Erhaltene Danke: 1



BeitragVerfasst: Sa 13.09.03 14:21 
huhu

evtl solltest hier

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
with myArray[i] do begin 
    with TButton.Create(self) do begin 
       Parent:=Form1; 
       Left:= 30
       Top:= 30*i; 
       Caption:='Butten'+IntToStr(i); 
    end
end;


das draus machen

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
myArray[i]:= TButton.Create(self); 
with myArray[i] do begin 
       Parent:=Form1; 
       Left:= 30
       Top:= 30*i; 
       Caption:='Butten'+IntToStr(i); 
end;


erst damit erzeugst du einen button in myArray[i]

beim freigeben brauchst du nicht extra visible:= false
wenn du es free'st verschwindets eh


Zuletzt bearbeitet von cbs am Sa 13.09.03 14:41, insgesamt 2-mal bearbeitet
delphisual Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 89

WIN 2000
D5 Prof
BeitragVerfasst: Sa 13.09.03 14:29 
Danke erstmal für deine Antwort, leider kriege ich immer noch die gleiche Fehlermeldung.

_________________
(Wer ein Problem definiert, hat es schon halb gelöst.)
cbs
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 207
Erhaltene Danke: 1



BeitragVerfasst: Sa 13.09.03 14:40 
hmm

ich hab mal eben das probiert

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
var
  Form1: TForm1;
  arr: array[0..4of TButton;

{...}

procedure TForm1.Button1Click(Sender: TObject);
var i: Integer;
begin
  for i:= 0 to 4 do begin
    arr[i]:= TButton.Create(form1);
    arr[i].Parent:= form1;
    arr[i].Top:= 10;
    arr[i].Left:= 10;
    arr[i].Caption:= 'test';
  end;
end;


es werden 5 buttons an der selben stelle erzeugt

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
procedure TForm1.Button2Click(Sender: TObject);
var i: Integer;
begin
  for i:= 0 to 4 do begin
    arr[i].Free;
  end;
end;


und wieder freigegeben
delphisual Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 89

WIN 2000
D5 Prof
BeitragVerfasst: Sa 13.09.03 14:54 
Deine Variante funktioniert wunderbar cbs :) , damit kann ich auch weiter arbeiten.
Besten Dank für die schnelle Antworten cbs :!:

_________________
(Wer ein Problem definiert, hat es schon halb gelöst.)