[fpc-pascal] Why does nested function get corrupted?

2022-09-08 Thread Hairy Pixels via fpc-pascal
Curious more than anything, if is nested captures state in a record and passes 
it as an hidden self param, why does returning the variable and calling from 
outside the calling stack frame corrupt the data? It seems like it technically 
should be there.

==

type
  TProc = procedure is nested;

function TestNested: TProc;
var
  data: integer;
begin
  data := 100;

  result := procedure
  begin
writeln(data);
  end;
end;

var
  proc: TProc;
begin
  proc := TestNested;
  proc();
end.

Regards,
Ryan Joseph

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Get TMethod from function reference

2022-09-08 Thread Hairy Pixels via fpc-pascal


> On Sep 8, 2022, at 11:14 AM, Michael Van Canneyt via fpc-pascal 
>  wrote:
> 
> It must keep this information somewhere, but this does not mean you can 
> typecast it to a TMethod.

Indeed. I wonder if this information could be exposed by some method in the 
interface? It would be useful to introspect the target class like you can do 
with “of object”.

Regards,
Ryan Joseph

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Get TMethod from function reference

2022-09-08 Thread Michael Van Canneyt via fpc-pascal




On Thu, 8 Sep 2022, Hairy Pixels via fpc-pascal wrote:





On Sep 8, 2022, at 9:09 AM, Michael Van Canneyt  wrote:

To me it seems logical that you cannot do this, since a function reference is 
actually an
interface, and there is no actual object to back it up.

Your function reference could also be a regular procedure (no data at all) or 
even a local
procedure.


I thought it keeps the method data in the interface so it could dispatch it 
later. How would it work otherwise?


It must keep this information somewhere, but this does not mean you can 
typecast it to a TMethod.

Michael.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Get TMethod from function reference

2022-09-08 Thread Hairy Pixels via fpc-pascal



> On Sep 8, 2022, at 9:09 AM, Michael Van Canneyt  
> wrote:
> 
> To me it seems logical that you cannot do this, since a function reference is 
> actually an
> interface, and there is no actual object to back it up.
> 
> Your function reference could also be a regular procedure (no data at all) or 
> even a local
> procedure.

I thought it keeps the method data in the interface so it could dispatch it 
later. How would it work otherwise?

Regards,
Ryan Joseph

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Get TMethod from function reference

2022-09-08 Thread Michael Van Canneyt via fpc-pascal



On Thu, 8 Sep 2022, Hairy Pixels via fpc-pascal wrote:


A function reference can call a class method but can you get the TMethod data 
from function references? It was possible with “is object” to cast to TMethod 
but that doesn’t seem to be possible with references.

=

type
 TMyClass = class
   constructor Create;
   procedure DoThis;
 end;

 constructor TMyClass.Create;
 var
   proc: reference to procedure;
 begin
   proc := @DoThis;
   writeln(Assigned(PMethod(proc)^.data)); // FALSE
 end;


To me it seems logical that you cannot do this, since a function reference is 
actually an
interface, and there is no actual object to back it up.

Your function reference could also be a regular procedure (no data at all) or 
even a local
procedure.

Michael.___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


[fpc-pascal] Get TMethod from function reference

2022-09-08 Thread Hairy Pixels via fpc-pascal
A function reference can call a class method but can you get the TMethod data 
from function references? It was possible with “is object” to cast to TMethod 
but that doesn’t seem to be possible with references.

=

type
  TMyClass = class
constructor Create;
procedure DoThis;
  end;

  constructor TMyClass.Create;
  var
proc: reference to procedure;
  begin
proc := @DoThis;
writeln(Assigned(PMethod(proc)^.data)); // FALSE
  end;


Regards,
Ryan Joseph

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Access Violation When SetLength(DynArray, Value)

2022-09-08 Thread Anthony Walter via fpc-pascal
> curious minds want to know: what was the fix?

In a separate part of the pool table initialization, I was precalculating
the midpoints and normals for bumper rails. I had carelessly written this
code:

  for I := 0 to Length(Rails) do
RailInit(Rails[I], I);
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Access Violation When SetLength(DynArray, Value)

2022-09-08 Thread wkitty42--- via fpc-pascal

On 9/8/22 9:54 AM, Anthony Walter via fpc-pascal wrote:

Please ignore this post. I fixed the issue.


curious minds want to know: what was the fix?


--
 NOTE: No off-list assistance is given without prior approval.
   *Please keep mailing list traffic on the list where it belongs!*
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Access Violation When SetLength(DynArray, Value)

2022-09-08 Thread Anthony Walter via fpc-pascal
Please ignore this post. I fixed the issue.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Access Violation When SetLength(DynArray, Value)

2022-09-08 Thread Peter B via fpc-pascal

I suggest trying without optimisations and/or using cmem, to see if that 
changes the outcome.

Also, if the array is corrupted prior to the setlength, then iterating the 
array with a trivial
  with... Writeln(Color)
or whatever, should trigger an exception.

That could then be used at various parts of the program to identify the point 
of corruption,
which might seem unrelated to array in question if the heap is getting messed 
up somehow.


Cheers,
Peter

P.S.

I had a strange (possible) heap corruption issue with a large program a year 
ago that broke stringlists.
I never found the cause, and after many minor code changes, the problem just 
dissappeared.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Access Violation When SetLength(DynArray, Value)

2022-09-08 Thread Jonas Maebe via fpc-pascal

On 2022-09-08 09:30, Anthony Walter via fpc-pascal wrote:

Is there a known edge case issue connected to setting the length of 
dynamic arrays?


No, and the location where you're getting the crash suggests an issue in 
your program that has corrupted the heap manager state.



Jonas
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


[fpc-pascal] Access Violation When SetLength(DynArray, Value)

2022-09-08 Thread Anthony Walter via fpc-pascal
Is there a known edge case issue connected to setting the length
of dynamic arrays?

I have a program that simulates a billiards game, and during the rerack
balls method of TPoolTable the number of pool balls on the table varies
based on the rerack option used. Currently, I am having a problem where an
exception is thrown if I try to SetLenth(Balls, 10) or greater than 10.
Values less than 10 work fine, but at 10 balls and above an exception is
thrown right at the SetLength call.

External: SIGSEGV

0041DCA4 eb12 jmp0x41dcb8

0041DCA6 4c89f0   mov%r14,%rax
0041DCA9 488b5008 mov0x8(%rax),%rdx
0041DCAD 488b4018 mov0x18(%rax),%rax
0041DCB1 488982a800   mov%rax,0xa8(%rdx)
^ instruction pointer is at this line ^

I tried compiler both FPC 3.3.1 from trunk source and 3.2.2 from the
official Sourceforge binary.  The same error occurs when building with both
compilers. My platform is x86_64 Linux.

For reference here is the type of Balls:

  TVec2 = record
 X, Y: Single;
  end;

  TPoolBall = record
Pos: TVec2;
Dir: TVec2;
Speed: Double;
Color: LongWord;
Touched: Boolean;
Sinking: Single;
SinkPocket: Integer;
SinkPos: TVec2;
Pocketed: Boolean;
Index: Integer;
  end;

  TPoolBalls = array of TPoolBall;

... later inside TPoolTable ...

  Balls: TPoolBalls;
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal