A peculiar question in Lazarus Release Candidate 3 of 4.0 lazarus / 21-General ---------------------------------------------------------------------- snowyforest · 2025-04-10 12:23 Why is the variable i  equal to 8? Is it because of the usage of SetLength() earlier? ---------------------------------------------------------------------- Martin_fr · 2025-04-10 12:53 1) Did you compile with/without optimization? Optimzation 1 works normally, but better is to switch optimisation off entirely.   (Also make sure you don't have any {$optimization ...} directives. With -O2 -O3 -O4 -Or  value will be show wrong. If this is in a package, check the settings for the package. 2) Probably ok: Is the correct stack selected? This only would be an issue, if you did select a different stackframe yourself. 3) Which debugger are you using? FpDebug or gdb ---------------------------------------------------------------------- snowyforest · 2025-04-10 13:48 Thank you very much for your comprehensive guidance. I completely disabled the optimization. I did not deliberately choose the stack. Regarding the debugger, I selected the default settings because I didn't know which one would be more appropriate. If possible, please give me some suggestions. Additionally, I would like to ask, is this a problem that has existed in the compiler ? ---------------------------------------------------------------------- Martin_fr · 2025-04-10 14:25 About the choice of debugger - https://wiki.lazarus.freepascal.org/Debugger_Setup#Choosing_the_corr ect_debugger_backend - https://wiki.freepascal.org/Debugger_Status If you are on Windows or Linux, and have an Intel/AMD CPU (x86 or i386) then you should use FpDebug. It is the most reliable in this case. About Optimization: That is mostly an issue in the compiler. For example with -O2 (or -Or) the compiler generates code that stores local variables in CPU- registers. But the compiler does not tell the debugger about it. So those variables then can't be watched/inspected. The compiler may also inline code, or optimize across source lines, then stepping may not always indicate the correct line. With optimization off the compiler is much better at generating the correct debug info. With -O1 it will work at 99.9% as good as with optimization off. As far as I know, the only side effect is, in some rare cases local vars will show their updated value one source line later than they should. In fact last I saw that was even an older fpc version. It may be ok in 3.2.2. Note that optimization is different for the project and each package. If you change settings in "project options", then code in packages will not follow that. For that you need to change the option of each package. (Or use "Additions and overrides", please search the wiki) And if your code contains https://www.freepascal.org/docs- html/prog/progsu58.html#x65-640001.2.58 then well, ... Stackframes should be self explaining.  If you open menu: View > Debug Windows > call stack: and you select a different stackframe, then values shown are for the selected procedure, and not where you are paused. If you stopped due to an exception => check that the correct frame is selected. If you stopped at a breakpoint (or if you did F7/F8) then you should always automatically have the correct stackframe. About your     i = 8    As far as I can tell the value should not be 8. If you were paused after the loop, then it might (then it may have any value...). But you are definitely paused inside the loop, and inside the loop it should be between 0 and 7 (as "numvars - 1" is 7). Please check the callstack window. The topmost entry in the callstack should be function "Gradient" at line 136. And it should be selected (there should be a small green arrow / or actually a red breakpoint with green arrow) in front of it. Please let me know: - your fpc version - your OS - CPU type (intel / arch / M1,M2...) / bitness 32/64 - debugger choice (see top of the message) From the screenshot, I think you are on Windows, and you do use FpDebug. Also if nothing above has helped with the issue: - While paused at that line in that loop - Open the asm window (view > debug windows > assembler) - you will see blocks of asm for each line of your source. - select and copy the lines for the entire loop - paste that asm here ---------------------------------------------------------------------- Martin_fr · 2025-04-10 14:37 Quote from: Martin_fr on April 10, 2025, 04:25:04 pmBut you are definitely paused inside the loop, and inside the loop it should be between 0 and 7 (as "numvars - 1" is 7). Well, actually... If you have code that writes to random memory (e.g. dangling pointer), then the value of "i" could have been corrupted by your code. That would have had to happen inside "GetGrad1d". Also in the following "bad code" it could be Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1;{$Mode objfpc} procedure Foo;var i: integer;  function Bar: integer;  begin    i := 8;   // invalid write to the loop variable, but not caught by fpc    result := i;  end;begin  for i := 0 to 7 do  begin    writeln(Bar);  end;end; begin  Foo;  readln;end.  But to see either of those,  in the debugger, while paused on the line in the loop, you need to either - step asm instructions - step INTO bar, then step out ---------------------------------------------------------------------- Martin_fr · 2025-04-10 14:41 You could try to switch between FpDebug and GDB, to see if it makes a difference. That would help to detect if it was a bug in the IDE debugger. ---------------------------------------------------------------------- ccrause · 2025-04-10 17:06 Quote from: Martin_fr on April 10, 2025, 04:25:04 pmBut you are definitely paused inside the loop, and inside the loop it should be between 0 and 7 (as "numvars - 1" is 7). In the original post the image shows that the for loop has been edited but not yet saved (line 134 is marked with yellow in the gutter).  Normally Lazarus would automatically save changes before compiling, therefore it is possible that what is shown in the editor is not the actual code that is executing. @snowyforest to clarify what happens if the code is saved, then re-built, and then report what the value of i is during each iteration of the for loop. ---------------------------------------------------------------------- Martin_fr · 2025-04-10 18:18 Ah, good catch, thanks @ccrause. If you edit code (e.g. to fix something) then the changes will not be part of the app, until you - stopped the current debug/run - recompiled - started it again So if for example you added the "-1" because you realised it was missing, and then wait for the code to be executed again (without ever stopping and recompiling), then the "-1" will not be executed. ---------------------------------------------------------------------- snowyforest · 2025-04-10 22:49 Thank you, Martin_fr, for your detailed and enthusiastic guidance. I have gained a lot of very valuable information from it. At the same time, I would like to thank ccrause for his valuable suggestions.