Default parameter not initialized lazarus / 21-General ---------------------------------------------------------------------- Birger52 · 2021-01-30 18:25 I'm having a slight problem. Basically a TList of objects that each holds a TStringList in a control. I have a function that reads a string when needed; it will usually be the first (index 0 in the TStringList), and in the selected (FSelected) TList Item: 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 function TTableContainer.GetSelectedText(aPos:integer=0) : string;begin  result := GetTextAt(FSelected, aPos);end; function TTableContainer.GetTextAt(aIdx:integer;aPos:integer=0) : string;begin  //  aIdx er index i Item list - bemæk at aPos checkes af TTableItem  if (aIdx > -1) and (aIdx < FItems.Count) then    result := TTableItem(FItems[aIdx]).StringAt(aPos)  else result := '';end; I use a call to GetSelectedText without any parameters 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   if EditBanIp(BanDataTable.GetSelectedText, true, false) then ShowBanData;  - but aPos is not set to 0 when calling GetTextAt; see attached image. (value is checked so function(s) simply returns an empty string, so no errors as such, but the function is not doing what it should) Kind of baffled - I have been using this function many many times, even in the same application - but here it does not function... What can cause a default parameter to not be set, when no value is given for the parameter in a function call? ---------------------------------------------------------------------- MarkMLl · 2021-01-30 19:16 Try stepping into GetTextAt() to see whether aPos doesn't contain anything until it's actually needed. MarkMLl ---------------------------------------------------------------------- Birger52 · 2021-01-30 20:14 I did - when call to GetSelectedText is with no value aPos is the number in the image - 1200392 - and it's the same every time. The stringAt function of the item looks like this: 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 function TTableItem.StringAt(aIdx : integer) : string;begin  if Assigned(Data) and (aIdx > -1) and (aIdx < Data.Count) then result := Data[aIdx]  else result := '';end;If GetSelectedText is called with no parameters, aIdx is 1200392 - otherwise it is the value of the call. ---------------------------------------------------------------------- Bart · 2021-01-30 20:24 Add a debugln() statement to write the value of aPos to the console (on Windows: compile with -WG- IIRC). Bart ---------------------------------------------------------------------- jamie · 2021-01-30 20:47 The Debug image looks a little suspicious because it has a VAR aPOS:Integer ///./// on it where as the define for the function does not. That tells me the compiler is seeing a variable of the same name elsewhere. EDIT  What we need to see is the Assembler dump at the point where the function gets called ---------------------------------------------------------------------- lucamar · 2021-01-30 21:03 Quote from: jamie on January 30, 2021, 09:47:49 pmThat tells me the compiler is seeing a variable of the same name elsewhere. No, look at the source position: it points to the function header. I think the suggestion to add DebugLn() before the call or even a simple ShowMessage() is good: it'll tell us what is the real value of aPos, regadless of what the debugger may think. ---------------------------------------------------------------------- howardpc · 2021-01-30 21:07 A simple class with nested function calls both with default parameters performs here as expected. You'll need to share a compilable example that shows the problem for others to help you see the cause of the problem. ---------------------------------------------------------------------- Birger52 · 2021-01-31 12:32 Thx for taking the time... aPos is used as the name for the variable in function calls, to identify the position of a given string in the TStringList of a TTableItem in many places. But only in these function calls. Not in the control, and not in the application (haven't actually checked other controls in the app - but it is nowhere used globally, so should not be a - or THE - problem). Tried to use the DebugLn() idea, but can't make that function. I get a ''Identifier not found" message. Tried ShowMessage. Had to add Dialogs to the Controls uses. 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 function TTableContainer.GetSelectedText(aPos:integer=0) : string;begin  ShowMessage(IntToStr(aPos));  result := GetTextAt(FSelected, aPos);end;It write a 0. Commented out the Showmessage line. The value of aPos is now 0 when it should be... Removed Dialogs from control - and the problem is still gone. So problem vaporized, by adding some code and removing it again.... Not really satisfactory or inducing confidence. ---------------------------------------------------------------------- MarkMLl · 2021-01-31 12:52 Did you tell the IDE to do a "Clean Directory" at any point? MarkMLl ---------------------------------------------------------------------- Birger52 · 2021-01-31 13:29 No - not that I know of, anyway. But after I posted the last post, it stopped compiling my project. Closing Lazarus it asked if I wanted to abort compiling the IDE - (which was not something I had asked it to do...). I said yes, and had to use JobList to shut down Lazarus. Restarted Lazarus, rebuid Lazarus manually, and everything seems to be doing OK. ---------------------------------------------------------------------- MarkMLl · 2021-01-31 14:14 In a different thread I promised that I'd look at a problem somebody was having where the compiler appeared to be noticing if included files had changed... I've not done that yet due to pressure of other things for which I'm duly contrite. I'm obviously not saying that it's the same problem, but there have in the past been various situations where .ppu files had to be deleted manually to fix e.g. compiler-version compatibility problems. MarkMLl ---------------------------------------------------------------------- Blaazen · 2021-01-31 15:17 You can take a look to my bugreport https://bugs.freepascal.org/view.php?id=37856: Quick Compile (F9) ignores changes in code and Clean Up and Build... is necessary. It happens with FPC trunk. Maybe the demo project can be even simplified. Anyway, this can lead to these kind of bugs. You add some code, then remove it and the original code starts work mysteriously. ---------------------------------------------------------------------- MarkMLl · 2021-01-31 15:22 I've also just added a bit of test code at https://forum.lazarus.freep ascal.org/index.php/topic,53028.msg392565.html#msg392565 but am having trouble duplicating the problem. I'm pretty sure I've seen it with a full compilation on 3.2.0, and the OP of that thread said it also happened on 3.0.4. MarkMLl ---------------------------------------------------------------------- Birger52 · 2021-01-31 16:55 Looks like it could be a rebuild problem then. Info in my signature is correct: Win7 64bit, Lazarus 2.0.8, FPC 3.0.4 Other than that, I don't really feel qualified to participate in debug tracking...