Referencing array foreach loop control variable by @ works wrong…
Original Reporter info from Mantis: roman.horvath
-
Reporter name: Roman Horváth
Original Reporter info from Mantis: roman.horvath
- Reporter name: Roman Horváth
Description:
I found an interesting bug connected to the dynamic arrays in Free Pascal.
To introduce the story and explain it a little: I am a teacher, and we program the class Sprite currently. One of the demos is to show how we can click on the sprite, activate it, and move it with another click. The bug is connected to the @ operator. When we use it to the foreach control variable within the loop body, it always points to the last element no matter what, which is wrong.
Steps to reproduce:
Please consider the following two fragments of code (to be found in hlavneOkno.pas, in the method TForm1.FormClick in the attached project):
// This variant does not work.
// The @Ball reference always points
// to the last element no matter what…
(* * )
for Ball in Balls do
if Ball.ptIn(ptMouse) then
begin
Current := @Ball;
exit
end;
( * *)
// This variant does work.
(* *)
for i := 0 to 2 do
if Balls[i].ptIn(ptMouse) then
begin
Current := @Balls[i];
exit
end;
(* *)
Mantis conversion info:
- Mantis ID: 38724
- OS: Windows
- OS Build: 10.0.19042.870
- Build: 2020/07/07 for x86_64
- Platform: Windows (PC)
- Version: 3.2.0