Autor Beitrag
Lemon
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 96

Win XP
D5 Standart
BeitragVerfasst: Di 30.12.03 12:02 
hallo,

ich habe hier folgenden quelltext um alle laufwerke anzuzeigen. leider bekomme ich den fehler zugriffsverletzung.

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
procedure GetDrives;
var
  Drives : array [1..255] of char;
  laufwerke0 : TStringList;
  i : Integer;
  laenge: DWord;
begin
  laufwerke0 := TStringList.Create;

  {Alle Laufwerke ermitteln}
  laenge := GetLogicalDriveStrings(255,@Drives);
  for i := 1 to laenge-2 do
  begin
    if (i mod 4) = 1 then laufwerke0.Add(copy(Drives[i],0,3));
  end;

  laufwerke0.free;
  Form1.Label1.Caption := laufwerke0[0];
  Form1.Label1.Caption := laufwerke0[1];
end;



kann mir da jemand helfen??
vielen dank schon mal im vorraus.

Moderiert von user profile iconMotzi: Delphi-Tags hinzugefügt.
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Di 30.12.03 12:06 
Du kannst nicht in den letzten beiden Zeilen auf "laufwerke0" zugreifen, wenn Du es eine Zeile weiter oben schon freigegeben hast.

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
Motzi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2931

XP Prof, Vista Business
D6, D2k5-D2k7 je Prof
BeitragVerfasst: Di 30.12.03 12:07 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
procedure GetDrives;
var
  Drives : array [1..255] of char;
  laufwerke0 : TStringList;
  i : Integer;
  laenge: DWord;
begin
  laufwerke0 := TStringList.Create; // <- hier erzeugst du das StringList-Objekt

  {Alle Laufwerke ermitteln}
  laenge := GetLogicalDriveStrings(255,@Drives);
  for i := 1 to laenge-2 do
  begin
    if (i mod 4) = 1 then laufwerke0.Add(copy(Drives[i],0,3));
  end;

  laufwerke0.free; // <- hier gibts du das StringList-Objekt frei
  Form1.Label1.Caption := laufwerke0[0]; // hier und
  Form1.Label1.Caption := laufwerke0[1]; // hier greifst du nochmal auf das bereits freigegebene Objekt zu
end;

Ich hab mal ein paar Kommentare in deinen Code eingefügt. Dein Fehler ist, dass du das Objekt zu früh freigibst, du willst ja schließlich nachher nochmal darauf zugreifen...!

_________________
gringo pussy cats - eef i see you i will pull your tail out by eets roots!
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Di 30.12.03 12:11 
Ätsch, zu langsam! :tongue:

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
Lemon Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 96

Win XP
D5 Standart
BeitragVerfasst: Di 30.12.03 13:47 
vielen dank.
hat super funktioniert!