Autor Beitrag
Arne
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 26



BeitragVerfasst: Mo 22.09.08 06:28 
Guten Morgen,

habe momentan ein Problem mit TFileStream. Ich will records vom Typ TFilterItem laden und per TList.Add in einer TList abspeichern.
Der erste record wird gelesen, aber beim zweiten (von vier) rocords erhalte ich eine Access violation. Vermutlich hat es wohl was mit dem ersten Parameter von Read zu tun (buffer), aber ich weiß nicht, wie ich es am elegantesten mache. Einlesen in ein Bytearray funktioniert, aber dann muß ich ja aus dem Bytearray wieder händisch die record-Elemente einzeln in den record kopieren. Das möchte ich vermeiden.

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
var
  myIndex                    : Integer;
  myFileStream               : TFileStream;
  myFilterItem               : ^TFilterItem;  { TFilterItem ist ein record }
begin
     { TFileStream.Create war erfolgreich, wenn wir hierhin kommen }
     try
       for myIndex := 0 to (Integer(myFileStream.Size) Div Integer(sizeof(TFilterItem))) - 1 do begin
         New(myFilterItem);
         if Assigned(myFilterItem) then begin
           myFileStream.ReadBuffer(myFilterItem, sizeof(TFilterItem));  { hier kommt beim 2. Schleifendurchlauf die Access Violation }
           gFilterList.Add(myFilterItem);
         end;
       end;
     except
       Dispose(myFilterItem); [ Im Fehlerfall letztes New() rückgängig machen }
     { ... }


hat jemand eine Idee?

Thanx, Arne
Xentar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2077
Erhaltene Danke: 2

Win XP
Delphi 5 Ent., Delphi 2007 Prof
BeitragVerfasst: Mo 22.09.08 08:17 
Wie ist TFilterItem aufgebaut? Da ist nich zufällig ein String drin, oder?

_________________
PROGRAMMER: A device for converting coffee into software.
Arne Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 26



BeitragVerfasst: Mo 22.09.08 08:32 
TFilterItem sieht so aus:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
type
  TFilterType = (kReadAccess, kWriteAccess);

  TFilterItem = record  { all items of the filter list are made of this record }
    mType                    : TFilterType; { Type 1 contains mFilterActive, mReceiverAddress, mSenderAddress and mMemoryAddress        }
                                            { Type 2 contains     - " -            - " -            - " -    , mActuatorAddress, mTaskID}
    mFilterActive            : Boolean;     { true = filter is active | false = filter is not taken into account for filtering purposes }
    mReceiverAddress         : Byte;        { bus address of sender }
    mSenderAddress           : Byte;        { bus address of receiver }

    mMemoryAddress           : Cardinal;    { TYPE 1 : memory address to filter for }

    mActuatorAddress         : Byte;        { TYPE 2 : actuator's index to set value for }
    mTaskID                  : Byte;        { TYPE 2 : sub-item of actuator. see AspBus manual. Ranges from $00 to $0B }
  end;


Kein String drin.

bye, Arne
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Mo 22.09.08 09:50 
Ich seh 2 Dinge:

user profile iconArne hat folgendes geschrieben:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
var
  myIndex                    : Integer;
  myFileStream               : TFileStream;
  myFilterItem               : ^TFilterItem;  { TFilterItem ist ein record }
begin
     { TFileStream.Create war erfolgreich, wenn wir hierhin kommen }
     try
       for myIndex := 0 to (myFileStream.Size Div sizeof(TFilterItem)) - 1 do begin //Keinen Typecast hier!
         New(myFilterItem);
         if Assigned(myFilterItem) then begin
           myFileStream.ReadBuffer(myFilterItem^, sizeof(TFilterItem));  { hier kommt beim 2. Schleifendurchlauf die Access Violation }
//Klassischer Fall eines Buffer Overruns: Du hast den Pointer überschrieben, der mit New angelegt wurde.
//Bei Zeigern auf typen, muss man explizit dereferenzieren
           gFilterList.Add(myFilterItem);
         end;
       end;
     except
       Dispose(myFilterItem); [ Im Fehlerfall letztes New() rückgängig machen }
     { ... }


hat jemand eine Idee?

Thanx, Arne

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
Arne Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 26



BeitragVerfasst: Mo 22.09.08 10:29 
Danke das war es!
Ich bin davon ausgegangen, daß myFilterItem ja ein Pointer ist und die Read Methode als Buffer eine Adresse (= Pointer) haben will und es daher klappen müßte! Ich denke zu sehr in C :roll: