Autor Beitrag
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Do 25.12.03 13:32 
Hallo, Leute!

Ich habe folgendes Problem:
Ich habe ein Objekt namens "tData", welches unter anderem ein Array enthält. Folgend definiert:
ausblenden 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:
type tFilter = record
  feld : String;
  operator : String;
  wert : String;
  textFilter : Boolean;
end;

type tData = class(TObject)
  protected
  {...}
    filters : Array of TFilter;
  {...}
  public
    procedure resetFilters;
  end;

{...}

procedure tData.resetFilters;
begin
  SetLength(filters, 0);
end;
{...}

Bis hierhin funktioniert noch alles.

Nun verwende ich in meinem Programm die Komponente TjvLookOutButton aus der Jedi-Sammlung. Und beim Aufruf von "SetLength(filters, 0)" fängt diese an, wie wild irgendwelche Nachrichten abzuarbeiten und schließlich eine Speicherzugriffsverletzung zu produzieren. Dasselbe passiert auch bei "SetLength(filters, Length(filters)+1)". Aber immer in dieser Komponente.

Wie kann das sein? Wie kann ich es beheben oder zumindest umgehen?

Der Fehler tritt übrigens auch auf, wenn ich vor Aufruf von resetFilters ein Application.ProcessMessages einschiebe!

MfG
Peter

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".


Zuletzt bearbeitet von Christian S. am Do 25.12.03 15:33, insgesamt 1-mal bearbeitet
Motzi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2931

XP Prof, Vista Business
D6, D2k5-D2k7 je Prof
BeitragVerfasst: Do 25.12.03 14:02 
SetLength hat eigentlich keinen Zusammenhang mit irgendwelchen Nachrichten oder sonstwas in der Richtung..! In welchem Zusammenhang stehen denn der Button und deine Klasse? Was macht deine Klasse eigentlich?

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

Win 10
C# (VS 2019)
BeitragVerfasst: Do 25.12.03 14:15 
Ja, das mit dem SetLength hat mich auch gewundert. Meine Augen waren noch größer als die hier, als ich sah, wo der Fehler auftrat: :shock:

Tja, das ist der Grund, weshalb ich hier poste: sie stehen in keinem Zusammenhang.

Die Klasse verwaltet Daten. Sie enthält eine Variable, die in Form einer MLB-Datenbank die Daten enthält. Die MLB war mir aber zu umständlich zu benutzen, also habe ich noch die Klassse drum herum gebaut.
Funktionierte prima, bis zu diesem Punkt. Der Button hat wirklich rein gar keine Verbindung mit diesem Objekt

Verstehst Du jetzt, wie groß meine Augen wirklich waren? ;-)

_________________
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: Do 25.12.03 14:33 
Tja, sowas kenn ich... ;)
Aber irgendwo muss da trotzdem ein Fehler drin sein... zeig mal den ganzen Code deiner Klasse... an welcher Stelle rufst du denn resetFilters auf?

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

Win 10
C# (VS 2019)
BeitragVerfasst: Do 25.12.03 14:58 
Die Klasse ist ziemlich groß, deswegen habe ich die Teile, die eigentlich nichts damit zu tun haben könne, weggelassen. Der interface-Teil ist komplett.
So sieht's aus:

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:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
type tFilter = record
  feld : String;
  operator : String;
  wert : String;
  textFilter : Boolean;
end;

type tData = class(TObject)
  protected
    fdata : Tmlb2;
    maxID : Integer;
    filters : Array of TFilter;
    
    fUseFilter : Boolean;
    fLastStep : Boolean;
    fSaved : Boolean;

    function imFilter : Boolean;

    procedure SetData(const name, value : String);
    function getData(const name : String) : String;
  public
    constructor create;
    destructor free;
    procedure add;
    procedure delete(const id : String);
    procedure clear;

    procedure addFilter(const feld, operator, wert : String; const textFilter : Boolean);
    procedure resetFilters;
    property useFilter : Boolean read fUseFilter write fUseFilter;

    procedure LoadFromFile(const dateiname : String);
    procedure SaveToFile(const dateiname : String; overwrite : Boolean = true);

    procedure goFirst;
    procedure goPrev;
    procedure goNext;
    procedure goLast;
    procedure gotoID(const id : String);

    function isEmpty : Boolean;

    property data[const Name: string]: string read GetData write SetData;  default;
    property lastStep : Boolean read fLastStep;
    property saved : Boolean read fSaved;
  end;

implementation

constructor tData.create;
begin
  inherited;
  fdata := TMlb2.Create;
  fdata.AddField('id');
  maxID := 0;
  fSaved := True;
end;

destructor tData.free;
begin
  fdata.free;
end;

procedure tData.Add;
begin
  fData.AddRow;
  inc(maxID);
  fdata.SetData('id', IntToStr(maxID));
  fSaved := False;
end;

procedure tData.Delete(const id : String);
begin
  fData.GoFirst;
  while not fData.EndOfFile do
  begin
    if fData.GetData('id') = id then
    begin
      fData.RemoveRow;
      if StrToInt(id) = maxId then Dec(maxID);
      fSaved := False;

      exit;
    end;
    fData.GoNext;
  end;
end;

procedure tData.clear;
begin
  fData.Clear;
  maxID := 0;
  fSaved := False;
end;

procedure tData.AddFilter(const feld, operator, wert : String; const textFilter : Boolean);
begin
  SetLength(filters, Length(filters)+1);
  filters[High(filters)].feld := feld;
  filters[High(filters)].operator := operator;
  filters[High(filters)].wert := wert;
  filters[High(filters)].textFilter := textFilter;
end;

procedure tData.resetFilters;
begin
  SetLength(filters, 0);
end;

function tData.imFilter : Boolean;
VAR i : INTEGER;
    floatVer : Real;
begin
  for i:=0 to high(filters) do
  begin
    if filters[i].textFilter then
    begin
      if not fdata.MatchData(filters[i].feld, filters[i].operator, filters[i].wert) then
      begin
        result := false;
        exit;
      end;
    end else
    begin
      floatVer := StrToFloatDef(filters[i].wert,0);
      if not fData.MatchFloat(filters[i].feld, filters[i].operator, floatVer) then
      begin
        result := false;
        exit;
      end;
    end;
  end;
  result := true;
end;

procedure tData.gotoID(const id : String);
begin
  fData.SavePosition;

  fData.GoFirst;
  while (not (fData.GetData('id') = id)) and (not fData.EndOfFile) do
    fData.GoNext;

  if fData.GetData('id') = id then
  begin
    flastStep := True;
    exit;
  end else
  begin
    flastStep := False;
    fData.RestorePosition;
    exit;
  end;
end;

function tData.isEmpty : Boolean;
begin
  if not fUseFilter then
  begin
    result := fData.RowCount = 0;
    exit;
  end else
  begin
    if fData.RowCount = 0 then
    begin
      result := true;
      exit;
    end;
    fData.SavePosition;
    fData.GoFirst;
    while (not imFilter) and (not fData.EndOfFile) do fData.GoNext;

    if not imFilter then result := True else result := False;
  end;
end;

procedure tData.SetData(const name, value : String);
VAR temp : String;
begin
  temp := AnsiLowerCase(name);
  if temp = 'id' then exit;
  if fData.GetFieldIndex(temp) < 0 then exit;
  fData.SetData(temp, value);

  fSaved := False;
end;

function tData.getData(const name : String) : String;
VAR temp : String;
begin
  temp := AnsiLowerCase(name);
  if fData.GetFieldIndex(temp) < 0 then exit;

  result := fData.GetData(temp);
end;


Dann gibt es noch eine Klasse, die in diesem Zusammenhang wichtig sein könnte:
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:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
type tRelation = Class(Tobject)
  private
    db1, db2 : TData;
    fdata : TMlb2;
    fSaved : Boolean;
  public
    constructor create (const db1, db2 : Tdata);

    procedure add(const id1Value, id2Value : String);

    procedure SaveToFile(const dateiname : String; overwrite : Boolean = True);
    procedure LoadFromFile(const dateiname : String);

    procedure delete(const id1Value, id2Value : String);
    procedure clear;

    procedure setDb1Filter(const id2Filter : String);
    procedure setDb2Filter(const id1Filter : String);

    property saved : Boolean read fSaved; 
  end;

implementation

constructor tRelation.Create(const db1, db2 : TData);
begin
  self.db1 := db1;
  self.db2 := db2;


  fdata := TMlb2.Create;
  fData.AddField('id1');
  fData.AddField('id2');
  fSaved := True;
end;

procedure tRelation.add(const id1Value, id2Value: String);
begin
  fData.GoFirst;
  while not fData.EndOfFile do
  begin
    if (fData.GetData('id1') = id1Value) and (fData.GetData('id2') = id2Value) then
    exit;

    fData.GoNext;
  end;
  fData.AddRow;
  fData.SetData('id1', id1Value);
  fData.SetData('id2', id2Value);
  fSaved := False;
end;

procedure tRelation.LoadFromFile(const dateiname : String);
begin
  if FileExists(dateiname) then
  begin
    fData.LoadFromFile(dateiname);
    fSaved := True;
  end;
end;

procedure tRelation.SaveToFile(const dateiname : String; overwrite : Boolean = True);
begin
  if FileExists(dateiname) then
    if overwrite then DeleteFile(dateiname)
    else exit;

  fData.SaveToFile(dateiname);
  fSaved := True;
end;

procedure tRelation.delete(const id1Value, id2Value : String);
begin
  fData.GoFirst;
  while not fData.EndOfFile do
  begin
    if (fData.GetData('id1') = id1Value) and (fData.GetData('id2') = id2Value) then
    begin
      fData.RemoveRow;
      fSaved := False;
      exit;
    end;
    fData.GoNext;
  end;
end;

procedure tRelation.clear;
begin
  fData.Clear;
  fSaved := False;
end;

procedure tRelation.setDb1Filter(const id2Filter: String);
begin
  db1.resetFilters;
  fData.GoFirst;
  while not fData.EndOfFile do
  begin
    if fData.GetData('id2') = id2Filter then
      db1.addFilter('id','=',fData.GetData('id1'), true);
  end;
end;

procedure tRelation.setDb2Filter(const id1Filter : String);
begin
  db2.resetFilters;
  fData.GoFirst;
  while not fData.EndOfFile do
  begin
    if fData.GetData('id1') = id1Filter then
      db2.addFilter('id','=',fData.GetData('id2'), true);
  end;
end;


Aufgerufen wird zuerst tRelation.setDb2Filter, dort db2.resetFilters und in db2.ResetFilters wird ja dann SetLength(filters, 0) aufgerufen. Dort passiert dann der Fehler. Bis dorthin kommt er jedoch auf jeden Fall!

MfG
Peter

//edit: ach ja, tdata wird nicht direkt verwendet, sondern über eine abgeleitete Klasse. Allerdings kann das eigentlich auch nichts damit zu tun haben. Aber hier die abgeleitete Klasse:
ausblenden 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:
type tCD = class(TData)
  private
  public
    constructor create;
    procedure add(const nummer, titel, komponist, dirigent, orchester, einspielungsdatum: String);
  end;

implementation

constructor tCD.create;
begin
  inherited;
  fdata.AddField('nummer');
  fdata.AddField('titel');
  fdata.AddField('komponist');
  fdata.AddField('dirigent');
  fdata.AddField('orchester');
  fdata.AddField('einspielungsdatum');
end;

procedure tCD.add(const nummer, titel, komponist, dirigent, orchester, einspielungsdatum: String);
begin
  inherited add;
  fdata.SetData('nummer', nummer);
  fdata.SetData('titel', titel);
  fdata.SetData('komponist', komponist);
  fdata.SetData('dirigent', dirigent);
  fdata.SetData('orchester', orchester);
  fdata.SetData('einspielungsdatum', einspielungsdatum);
end;

_________________
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: Do 25.12.03 15:09 
Zitat:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
constructor tRelation.Create(const db1, db2 : TData);
begin
  self.db1 := db1;
  self.db2 := db2;


  fdata := TMlb2.Create;
  fData.AddField('id1');
  fData.AddField('id2');
  fSaved := True;
end;

Wie erzeugst du so ein Relation-Objekt? Welche Objekte übergibst du da als Parameter?
Vielleicht ist es am einfachsten wenn du mir das Projekt schickst und ich schau mir das ganze mal im Debugger an...

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

Win 10
C# (VS 2019)
BeitragVerfasst: Do 25.12.03 15:26 
ICH IDIOT!

Vergiss die Mail, die ich Dir geschickt habe. Vergiss diesen Thread. Vergiss mich.

Ohhhhhh, ich könnte ... ich weiß nicht, was ich könnte, .... boah. Ich übergebe die Objekte an Relation, bevor ich sie mit Create erzeugt habe.

_________________
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: Do 25.12.03 15:30 
Aja.. ;) sowas in der Richtung hab ich irgendwie schon vermutet.. ;) Daher auch meine vorherige Frage...! ;)

_________________
gringo pussy cats - eef i see you i will pull your tail out by eets roots!