[LAMW] Setlength causes the app to crash lazarus / 43-Android ---------------------------------------------------------------------- r.lukasiak · 2021-12-21 17:57 Hi everyone! I have pretty weird issue. My app crashes whenever I fire SetLength to resize an array, but what's even weirder it happens only with one variable, resizing other dynamic arrays goes smoothy. 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 typemessage = record    SMsgID: integer;    msgid:  integer;    FromID: string;  ToID:   string;    msg:    string;    mtype:  string;    tstamp: Tdatetime;    seen_stamp: Tdatetime;    status: DWORD;  end;    varMessages: array of message;... procedure ......begin  SetLength(Messages,l)end;casues the app to crash. I have another array of record with very similiar fields of integer, string and Tdatetime and there is no issue with it. I think it's not strictly LAMW problem, rather the compiler but maybe someone had a similar problem? Greetings! ---------------------------------------------------------------------- jmpessoa · 2021-12-21 18:14 Maybe, the compiler don't like that you use "message" or  "messages"   keyword... ---------------------------------------------------------------------- r.lukasiak · 2021-12-21 18:38 I haven't thought of that.... Anyway I changed the naming but it didn't help, it keeps crashing ---------------------------------------------------------------------- jmpessoa · 2021-12-21 18:41 Quote I changed the naming but it didn't help, it keeps crashing Did you do a "Run" -> "Clean Up and Build"  ? ---------------------------------------------------------------------- r.lukasiak · 2021-12-21 18:45 I've done it several times, anyway I tried it once again now but still no effect. ---------------------------------------------------------------------- jmpessoa · 2021-12-21 18:49 Already entering the despair phase....  Drop these fields from record ....  tstamp: Tdatetime;  seen_stamp: Tdatetime; and  "Run" -> "Clean Up and Build"  ... ---------------------------------------------------------------------- r.lukasiak · 2021-12-21 19:22 I've also already tried it... still no result. The other record consists of TDateTime and it's not causing any problems. ---------------------------------------------------------------------- engkin · 2021-12-21 19:23 I think r.lukasiak is deceiving us  He is leading us to conclude the exact same conclusions he has. Give us facts: 1-Logcat output: why did Android kill your app. 2-SetLength is not buggy. Are you calling it in a method or procedure? 3-What do you get for SizeOf the record? 4-What value do you have for l? ---------------------------------------------------------------------- r.lukasiak · 2021-12-21 20:22 QuoteI think r.lukasiak is deceiving us  hahhahah why would I do it if I'm desperately needing solution. I've been stuck at it for past 7h 1. I'm not sure what you mean where can I find it? 2. In a procedure 3. ShowMessage(IntToStr(Sizeof(Message))) returns 28, and at the moment it's.. 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 message = record    SMsgID: integer;    msgid:  integer;    FromID: string;    ToID:   string;    msg:    string;    mtype:  string;    status: DWORD;end;   4. l:=SQLiteCursor.GetRowCount, but either I use a variable or fixed value, it results in a crash. ---------------------------------------------------------------------- Seenkao · 2021-12-21 20:40 Quote from: r.lukasiak on December 21, 2021, 06:57:32 pmCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i message = record... Messages: array of message;...begin  SetLength(Messages,l)end;Вас просили убрать все message, Messages. Вероятнее всего это и есть ваша проблема. Сделайте так: Eng: You were asked to remove all message, Messages. This is most likely your problem. Do 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 MYmessage = record   //  <---... MYMessages: array of MYmessage;   //  <---...begin  SetLength(MYMessages,l);   //  <---end; ---------------------------------------------------------------------- r.lukasiak · 2021-12-21 20:47 yes and I did it when jmpessoa mentioned that but it didn't change anything :/ I changed naming to Spanish-like but let me use the naming you propose, in case FPC speaks Spanish ---------------------------------------------------------------------- r.lukasiak · 2021-12-21 20:58 latest test: 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 MYmessage = record    SMsgID: integer;    msgid:  integer;    FromID: string;    ToID:   string;    msg:    string;    mtype:  string;    status: DWORD;  end;   MyMessages: array of MYMessage;  procedure .....begin  SetLength(MYMessages,Kapp.SQLC.GetRowCount);end;   if I replace MYMessages with My_other_variable which is a very similar record of integers, strings etc, everything is fine o.O ---------------------------------------------------------------------- engkin · 2021-12-21 21:11 As for you being deceptive, I was kidding. 1-Search for "adb logcat" 2-Good. 3-Fine. 4-What do you expect to happen if l is 99999999999? Did you try a manual value for l of 1 or 2 for instance? Since you did not know about logcat, how did you come to the conclusion that the problem is in SetLength? i just did a quick android console app: 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 app4;  {$mode delphi}  uses  Classes, SysUtils; type  message = record      SMsgID: integer;      msgid:  integer;      FromID: string;      ToID:   string;      msg:    string;      mtype:  string;      status: DWORD;  end; var  Messages:array of message;  l,i:Integer; begin  WriteLn('SizeOf(message): ',SizeOf(message));  l:=1;  for i:=1 to 32 do  begin    WriteLn(i,'-L: ', l);    SetLength(Messages,l);    WriteLn('Passed...');    WriteLn('');    l:=l*2;  end;end. Its output: QuoteSizeOf(message): 28 1-L: 1 Passed... 2-L: 2 Passed... 3-L: 4 Passed... 4-L: 8 Passed... 5-L: 16 Passed... 6-L: 32 Passed... 7-L: 64 Passed... 8-L: 128 Passed... 9-L: 256 Passed... 10-L: 512 Passed... 11-L: 1024 Passed... 12-L: 2048 Passed... 13-L: 4096 Passed... 14-L: 8192 Passed... 15-L: 16384 Passed... 16-L: 32768 Passed... 17-L: 65536 Passed... 18-L: 131072 Passed... 19-L: 262144 Passed... 20-L: 524288 Passed... 21-L: 1048576 Passed... 22-L: 2097152 Passed... 23-L: 4194304 Passed... 24-L: 8388608 Passed... 25-L: 16777216 Passed... 26-L: 33554432 Passed... 27-L: 67108864 Passed... 28-L: 134217728 An unhandled exception occurred at $004D5664: ERangeError: Range check error   $004D5664 ---------------------------------------------------------------------- engkin · 2021-12-21 21:15 Is it a global or local variable? ---------------------------------------------------------------------- r.lukasiak · 2021-12-21 21:28 Quote4-What do you expect to happen if l is 99999999999?At the moment there are maybe 20-25 records in the database so l shouldn't be >25, anyway just to make sure  I tried with a fixed value of 1. QuoteDid you try a manual value for l of 1 or 2 for instance?yes, with l=1 and omitting l, just entering fixed value of 1. QuoteSince you did not know about logcat, how did you come to the conclusion that the problem is in SetLength?The error occurred after I wrote that procedure so first I commented it out entirely and it worked well. Then I was uncommenting line by line to pinpoint the culprit. 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 28-L: 134217728An unhandled exception occurred at $004D5664:ERangeError: Range check error  $004D5664it crashed at 134217728, it's way more than I'd need. I don't plan more than 50-100. QuoteIs it a global or local variable?it's global ---------------------------------------------------------------------- Josh · 2021-12-21 21:39 hi using l:=l*2; to setlength makes for a massive array with 28bytes per record at step  29 its nearly 4GB in size ---------------------------------------------------------------------- engkin · 2021-12-21 21:42 Do you use pointers in your code? Run your app Clear the log with adb logcat -c Start the log with errors using: adb logcat *:E Crash your app Stop the log using CTRL+C Paste the result here Like this post if you can. For more, check: adb logcat --help ---------------------------------------------------------------------- engkin · 2021-12-21 21:45 Quote from: josh on December 21, 2021, 10:39:33 pmhi using l:=l*2; to setlength makes for a massive array with 28bytes per record at step  29 its nearly 4GB in size Yes. Since we don't know why his app crashed, I was hoping for a big value in l. The other possibility, corrupt memory. In both cases SetLength is not the problem. His code has a bug somewhere else. ---------------------------------------------------------------------- r.lukasiak · 2021-12-21 21:52 interestingly, if I change the record fields to 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     ChatID:  string;    FromID:  string;    Msg:     string;    mtype:   string;  tstamp:  TDateTime;    status:  DWORD;everything is ok. It's exactly like the other variable. Well, anyway I already did the procedure in a different way, using just array of integer and the rest of data is taken directly from the database but it just makes me wonder what's wrong with that record  QuoteDo you use pointers in your code?no pointers. I'm googling for adb logcat but it seems to be for emulators, isn't it? I'm testing on a physical phone ---------------------------------------------------------------------- engkin · 2021-12-21 21:55 Quote from: r.lukasiak on December 21, 2021, 10:52:37 pmI'm googling for adb logcat but it seems to be for emulators, isn't it? I'm testing on a physical phone No. It works for the physical device. ---------------------------------------------------------------------- engkin · 2021-12-21 22:00 Your change does not reveal the bug. It is still there. It might show up again in a different way. Save a copy of your project where it crashes with Setlength. ---------------------------------------------------------------------- engkin · 2021-12-21 22:10 Just for my curiosity: What versions of FPC/Lazarus are you using? Is the cash happening on the first time you call Setlength with this variable? What optimization level you have for the project? ---------------------------------------------------------------------- r.lukasiak · 2021-12-21 22:34 QuoteWhat versions of FPC/Lazarus are you using?version: 2.0.12 r64642 + LAMW + aarch64 (installed using fpcdeluxe) QuoteIs the cash happening on the first time you call Setlength with this variable?yes. It's actually the only SetLength on this variable QuoteWhat optimization level you have for the project?-O3 QuoteYour change does not reveal the bug. It is still there. It might show up again in a different way. Save a copy of your project where it crashes with Setlength.I've just brought back the record fields and it worked... How the heck? ---------------------------------------------------------------------- r.lukasiak · 2021-12-21 22:44 Now I even changed the naming to the original Message/Messages and it's still working o.O ---------------------------------------------------------------------- engkin · 2021-12-21 23:35 Message/Messages is not a problem as I showed in my test above. Make a visual change in your code in the same file where you call Setlength to be certain it is compiled and included in your app. ---------------------------------------------------------------------- r.lukasiak · 2021-12-22 00:18 I already added some lines, compiled and tested several times. So far everything seems to be in tact. QuoteMessage/Messages is not a problem as I showed in my test above.yes, but it was one of ideas so I wanted to check it out, to make sure it's not the culprit. Thanks everyone for ideas, I'll leave the topic open, in case the problem appears again.