Recent

Author Topic: Access Violation when passing Objects  (Read 1782 times)

Weitentaaal

  • Hero Member
  • *****
  • Posts: 503
  • Weitental is a very beautiful garbage depot.
Access Violation when passing Objects
« on: January 27, 2022, 01:46:44 pm »
Hello,

so i got this Strange error like shown below:

If u take a look at the two images attached u can see that i just call a procedure, and when it gets called i get an access violation.

Thats the Object, which gets passed to the procedure->(which seems to be fine)
Code: Pascal  [Select][+][-]
  1. TEst1 = <TZUBEHOER> = {
  2.  
  3.   <TOBJECT> = {
  4.  
  5.     _vptr$ = {
  6.  
  7.       0xa6580c,
  8.  
  9.       0x1}},
  10.  
  11.   FMAXQUANTITY = 1,
  12.   FISCHECKED = false,
  13.   FGROUP = 0,
  14.   FQUANTITY = 0,
  15.   FISSELFDEF = false,
  16.   FID = 49,
  17.   FNAME = 'Ansaug-/Ausblashaube ALU',
  18.   FZUSATZPARAM = 0,
  19.   FNEEDID = '0',
  20.   FPREIS = 0}
  21.  

the Exception is not even in my code it gets fired in this line

Code: Pascal  [Select][+][-]
  1. begin
  2.  

does any1 know how to fix this ?

Edit: i have the Same function which ensures that items are deleted, which works perfectly fine


« Last Edit: January 27, 2022, 01:54:36 pm by Weitentaaal »
Lazarus: 2.0.12 x86_64-win64-win32/win64
Compiler Version: 3.2.2

Zvoni

  • Hero Member
  • *****
  • Posts: 2317
Re: Access Violation when passing Objects
« Reply #1 on: January 27, 2022, 02:05:38 pm »
The only difference i see is your Line 9 to your Watchwindow:
"0x1" vs "0x0"
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Weitentaaal

  • Hero Member
  • *****
  • Posts: 503
  • Weitental is a very beautiful garbage depot.
Re: Access Violation when passing Objects
« Reply #2 on: January 27, 2022, 02:17:40 pm »
All the Data displayed above (Which describes the Object) was taken when hovering over the Object "TEst1". So this should be the same as in the wachwindow. Is the Problem here ?

Edit: Just tested this Function in different Places of my Code.. always the same. Tested with a fresh created Object, with nil and with an object that was holding real data.
« Last Edit: January 27, 2022, 02:22:17 pm by Weitentaaal »
Lazarus: 2.0.12 x86_64-win64-win32/win64
Compiler Version: 3.2.2

Weitentaaal

  • Hero Member
  • *****
  • Posts: 503
  • Weitental is a very beautiful garbage depot.
Re: Access Violation when passing Objects
« Reply #3 on: January 27, 2022, 02:27:39 pm »
Just checked the assembler, even if i don't know how to use it, and saw that the "end;" line gets executed before the "begin" line. (See attachment). Could thi be the reason for the AV ?
Lazarus: 2.0.12 x86_64-win64-win32/win64
Compiler Version: 3.2.2

Weitentaaal

  • Hero Member
  • *****
  • Posts: 503
  • Weitental is a very beautiful garbage depot.
Re: Access Violation when passing Objects
« Reply #4 on: January 27, 2022, 03:40:22 pm »
Found the mistke i did:

i got an Array of integer (GroupID: Array of Integer;), which was filled by following procedure:

Code: Pascal  [Select][+][-]
  1. function TBauteil.ZubGetGroup(GroupID: Integer): TIntegerArray;
  2. var
  3.    i, j: Integer;
  4. begin
  5.    j:= 0;
  6.    for i := 0 to ZubList.Count do begin
  7.       if ZubList.Items[i].Group = GroupID then begin
  8.          Result[j]:= ZubList.Items[i].ID;
  9.          j:= j + 1;
  10.       end;
  11.    end;
  12. end;
  13.  

My Array was "Array of Integer" the Result of the Function was "TIntegerArray". They are not the same because TIntegerArray is not a Dynamic array. why won't i get a Syntax error there ? and why did the Exception occur in the begining on the "begin" line ? do i have wrong settings for that or is that intended?

Thanks for your work !
« Last Edit: January 27, 2022, 03:41:53 pm by Weitentaaal »
Lazarus: 2.0.12 x86_64-win64-win32/win64
Compiler Version: 3.2.2

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: Access Violation when passing Objects
« Reply #5 on: January 27, 2022, 03:55:13 pm »
My Array was "Array of Integer" the Result of the Function was "TIntegerArray". They are not the same because TIntegerArray is not a Dynamic array.
Well, that depends: many programmers redefine TIntegerArray as array of integer. But FPC has support for this in the types unit: TIntegerDynArray which is declared as Array of integer. So if you need the latter, simply add types and use TIntegerDynArray as result type. Because TIntegerArray redeclaration might confuse.
Specialize a type, not a var.

Weitentaaal

  • Hero Member
  • *****
  • Posts: 503
  • Weitental is a very beautiful garbage depot.
Re: Access Violation when passing Objects
« Reply #6 on: January 27, 2022, 04:01:57 pm »
Hey thanks for the clarification. i know how to use it now :)

But i still don't know why the error was shown at this certain line ? it was so confusing and took me like forever to find where the mistake was made.
Lazarus: 2.0.12 x86_64-win64-win32/win64
Compiler Version: 3.2.2

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: Access Violation when passing Objects
« Reply #7 on: January 27, 2022, 04:09:13 pm »
Oh, I forgot one more easy option, w/o the types unit:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. type TMyIntegerArray = specialize TArray<integer>; // defined in system as array of T
In newer versions of fpc TArrray<T> is declared in system.
And let that now be the option I use most.... :o %) With no particular reason, I am just fond of generics, I guess.
It has no code penalty over the other options.
« Last Edit: January 27, 2022, 04:12:37 pm by Thaddy »
Specialize a type, not a var.

Weitentaaal

  • Hero Member
  • *****
  • Posts: 503
  • Weitental is a very beautiful garbage depot.
Re: Access Violation when passing Objects
« Reply #8 on: January 27, 2022, 04:36:35 pm »
Thank you thaddy... appreciated ! 8)
Lazarus: 2.0.12 x86_64-win64-win32/win64
Compiler Version: 3.2.2

 

TinyPortal © 2005-2018