Autor Beitrag
Ardos
Hält's aus hier
Beiträge: 7



BeitragVerfasst: Mi 08.10.08 14:11 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
procedure FileLoader.LoadFile(Filepath:string);
VAR Stream:TFileStream;
begin
  Stream:=TFileStream.Create(Filepath,fmOpenRead or fmShareDenyWrite);
  try
    SetLength(fData,Stream.Size);
    Stream.Read(PChar(fData)^,Stream.Size);
  finally
    Stream.Free;
  end;
end;



Ich versuche so eine Datei einzulesen das klappt soweit ganz gut wenn ich die Prozedur aber zum 4.Mal aufrufe kommt Acces Violation wenn ich den Stream erstelle. Kann mir wer helfen?

Moderiert von user profile iconGausi: Code- durch Delphi-Tags ersetzt
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: Mi 08.10.08 14:19 
Wenn fData vom Typ String oder Array of Byte ist, sollte das so gehen:
ausblenden Delphi-Quelltext
1:
2:
3:
SetLength(fData, Stream.Size); 
Stream.Read(fData[1],Stream.Size); // bei Strings
Stream.Read(fData[0],Stream.Size); // bei Array of Byte

_________________
We are, we were and will not be.
Ardos Threadstarter
Hält's aus hier
Beiträge: 7



BeitragVerfasst: Mi 08.10.08 14:33 
nein macht keinen unterschied
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mi 08.10.08 14:39 
Was ist fData denn nun?
Ardos Threadstarter
Hält's aus hier
Beiträge: 7



BeitragVerfasst: Mi 08.10.08 14:40 
array of byte
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mi 08.10.08 14:51 
Das Problem konnte ich weder mit deinem Code noch mit user profile iconGausis Vorschlag reproduzieren, Testprojekt (nicht besonders sauber programmiert, aber egal) im Anhang...
Einloggen, um Attachments anzusehen!
Ardos Threadstarter
Hält's aus hier
Beiträge: 7



BeitragVerfasst: Mi 08.10.08 15:12 
ok danke euch habe herausgefunden dass, das Problem woanders liegen muss
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: Mi 08.10.08 15:19 
Vielleicht als Hinweis: Im Gegensatz zu den meisten anderen Konstruktoren kann der von TfileStream schon eine Exception auslösen. D.h. dein Try-except-Block muss eigentlich anders gestaltet werden. In etwa so:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
begin 
  Stream := Nil; // lokale Variable initialisieren
  {Edit: Stream auf Nil setzen, nicht TStream.}
  try
    Stream := TFileStream.Create(Filepath,fmOpenRead or fmShareDenyWrite); 
    try 
      // tu was mit Stream
    finally 
      if assigned(Stream) then
        Stream.Free; 
    end; 
  except
    ShowMessage('Kann Filestream nicht erzeugen...')
  end;
end;

_________________
We are, we were and will not be.


Zuletzt bearbeitet von Gausi am Mi 08.10.08 15:29, insgesamt 1-mal bearbeitet
DeddyH
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Mi 08.10.08 15:24 
Zitat:
ausblenden Delphi-Quelltext
1:
2:
if assigned(Stream) then
  Stream.Free;

Doppeltgemoppelt ;). IIRC prüft Free selbstständig auf Assigned.