Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Algorithmen, Datenstrukturen und Klassendesign (https://www.delphipraxis.net/78-algorithmen-datenstrukturen-und-klassendesign/)
-   -   Code Umstellung von D7 auf D11.3 (https://www.delphipraxis.net/214099-code-umstellung-von-d7-auf-d11-3-a.html)

MyRealName 15. Nov 2023 06:27

Code Umstellung von D7 auf D11.3
 
Hallo,

wir stellen gerade ältere Anwendungen um, die compilieren unter D7 und laufen seit Jahren reibungslos.
Nun unter D11.3 kriegen wir Bereichsüberschreitungen im folgenden Fall:

Man hat ein Array, 32 Einträge, es ist als Array of TColor definiert.
Jetzt wird das untypisiert übergeben, zum so

Code:
const TMyColors: Array of TColor;

procedure Foo(const Colors; Count: Integer)
begin
  For i := 0 To Pred(Count) Do
    TMyColors(Colors)[i] := clBlack;
end;
Der erste Eintrag geht noch, beim nächsten crasht es. Wir haben es jetzt so gelöst, das wir das jetzt mal typisiert übergeben, aber es wäre interessant zu wissen, warum es nicht mehr geht.

Das zweite Problem war, dass man (wenn auch vllt. fälschlicherweise) einem Word ein Longint zuweist, das unter Delphi 7 ging, der obere Beweich dann halt abgeschnitten wird. Unter 11.3 crasht es :O

hhcm 15. Nov 2023 06:37

AW: Code Umstellung von D7 auf D11.3
 
Ich hatte mal etwas ähnliches. Da war in einer inc Datei folgendes definiert.

Delphi-Quellcode:
{$IFDEF VER150}
{$R-}
{$ENDIF}

Sinspin 15. Nov 2023 08:19

AW: Code Umstellung von D7 auf D11.3
 
Von D7:shock:
Viele Datentypen haben sich geändert. Aufpassen was TColor jetzt ist. Am besten die alten Typen fürs erste mit übernehmen (also aus D7 kopieren) bis es wieder läuft und erst im zweiten Schritt ersetzen.
Sowas wie
Delphi-Quellcode:
const Colors;
würde ich mir verkneifen das führt ohne ordentliche Dokumentation eh früher oder später zu schönen Fehlern.

Kas Ob. 15. Nov 2023 10:31

AW: Code Umstellung von D7 auf D11.3
 
Code:
const TMyColors: Array of TColor;
How did this even compile ?

Also "Array of" is managed type, so there is possibility the compiler did adjusted its reference count when you type casted it, so it could be freed when the code run the first time and the second raised AV.

Also what do you mean by crash ? what is the exact message ?

Sorry for using English.

dummzeuch 15. Nov 2023 10:39

AW: Code Umstellung von D7 auf D11.3
 
Zitat:

Zitat von MyRealName (Beitrag 1529695)
Hallo,

wir stellen gerade ältere Anwendungen um, die compilieren unter D7 und laufen seit Jahren reibungslos.
Nun unter D11.3 kriegen wir Bereichsüberschreitungen im folgenden Fall:

Man hat ein Array, 32 Einträge, es ist als Array of TColor definiert.
Jetzt wird das untypisiert übergeben, zum so

Code:
const TMyColors: Array of TColor;

procedure Foo(const Colors; Count: Integer)
begin
  For i := 0 To Pred(Count) Do
    TMyColors(Colors)[i] := clBlack;
end;
Der erste Eintrag geht noch, beim nächsten crasht es. Wir haben es jetzt so gelöst, das wir das jetzt mal typisiert übergeben, aber es wäre interessant zu wissen, warum es nicht mehr geht.

Wie sieht denn die Initialisierung von Colors aus? Ist das überhaupt ein Array mit der Länge Count?

MyRealName 15. Nov 2023 11:11

AW: Code Umstellung von D7 auf D11.3
 
Ja, es wird initialisiert und man fragt Länge ab. Das wird dann als 2. Param ja über geben und er versucht dann, über die For-Schleife alle elemente zu lesen, aber angeblich gibt es nur 1 Element. Deswegen Fehler bei der Bereichsüberprüfung.

Uwe Raabe 15. Nov 2023 11:16

AW: Code Umstellung von D7 auf D11.3
 
Kannst du das auf ein Minimalbeispiel runterbrechen. So kompliziert sieht das ja erstmal nicht aus.

Delphi.Narium 15. Nov 2023 11:30

AW: Code Umstellung von D7 auf D11.3
 
bin mal ein bisserl naiv, könnte es denn mit demda funktionieren (hab' halt nur D7 und da geht das)?
Delphi-Quellcode:
type TMyColors = Array of TColor;

procedure Foo(const colors);
var
  i : Integer;
begin
  for i := low(tMyColors(Colors)) to high(tMyColors(colors)) do
   tMyColors(Colors)[i] := clBlack;
end;

procedure DoIt;
var
  Colors : TMyColors;
begin
  SetLength(Colors,10);
  foo(Colors);
end;

Kas Ob. 15. Nov 2023 11:52

AW: Code Umstellung von D7 auf D11.3
 
Zitat:

Zitat von Delphi.Narium (Beitrag 1529724)
bin mal ein bisserl naiv, könnte es denn mit demda funktionieren (hab' halt nur D7 und da geht das)?
Delphi-Quellcode:
type TMyColors = Array of TColor;

procedure Foo(const colors);
var
  i : Integer;
begin
  for i := low(tMyColors(Colors)) to high(tMyColors(colors)) do
   tMyColors(Colors)[i] := clBlack;
end;

procedure DoIt;
var
  Colors : TMyColors;
begin
  SetLength(Colors,10);
  foo(Colors);
end;

That is perfectly fine and will work, though very not recommended, because after all that, just declare the type, don't leave it untyped, and you will never have a problem with any compiler.


Alle Zeitangaben in WEZ +1. Es ist jetzt 12:22 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz