Lazarus

Programming => Operating Systems => Android => Topic started by: Handoko on January 18, 2018, 06:33:41 pm

Title: A LAMW Bug?
Post by: Handoko on January 18, 2018, 06:33:41 pm
Can someone please tell me it is a bug in LAMW or something wrong in my code?

I was writing a game using LAMW. Everything was okay, but until I found if I press the the jCanvasES2 component repeatedly very fast then my app will crash. It is 100% reproducible.

Here is the code after removing all the unnecessary things:
Code: Pascal  [Select][+][-]
  1. {Hint: save all files to location: /home/handoko/Desktop/MicroWorld/jni }
  2. unit unit1;
  3.  
  4. {$mode delphi}
  5.  
  6. interface
  7.  
  8. uses
  9.   Classes, SysUtils, AndroidWidget, Laz_And_Controls, Laz_And_GLESv2_Canvas,
  10.   Laz_And_GLESv2_Canvas_h;
  11.  
  12. type
  13.  
  14.   { TAndroidModule1 }
  15.  
  16.   TAndroidModule1 = class(jForm)
  17.     jButton1: jButton;
  18.     jCanvasES2_1: jCanvasES2;
  19.     jTimer1: jTimer;
  20.     procedure AndroidModule1Close(Sender: TObject);
  21.     procedure jButton1Click(Sender: TObject);
  22.     procedure jCanvasES2_1GLDraw(Sender: TObject);
  23.     procedure jTimer1Timer(Sender: TObject);
  24.   private
  25.     var
  26.       grScaleX, grScaleY: Single; // Used internally for graphics engine
  27.     procedure grCalculateScaleValues;
  28.   end;
  29.  
  30. var
  31.   AndroidModule1: TAndroidModule1;
  32.  
  33. implementation
  34.  
  35. {$R *.lfm}
  36.  
  37. { TAndroidModule1 }
  38.  
  39. procedure TAndroidModule1.AndroidModule1Close(Sender: TObject);
  40. begin
  41.   jTimer1.Enabled := False;
  42. end;
  43.  
  44. procedure TAndroidModule1.jButton1Click(Sender: TObject);
  45. begin
  46.   jTimer1.Enabled := not(jTimer1.Enabled);
  47. end;
  48.  
  49. procedure TAndroidModule1.jCanvasES2_1GLDraw(Sender: TObject);
  50. var
  51.   i: Integer;
  52. begin
  53.   jCanvasES2_1.Screen_Setup (jCanvasES2_1.Width, jCanvasES2_1.Height, xp2D,cCull_YES);
  54.   jCanvasES2_1.Screen_Clear(1,1,1,1);
  55.   grCalculateScaleValues;
  56. end;
  57.  
  58. procedure TAndroidModule1.jTimer1Timer(Sender: TObject);
  59. begin
  60.   jCanvasES2_1.Refresh;
  61. end;
  62.  
  63. procedure TAndroidModule1.grCalculateScaleValues;
  64. begin
  65.   if (jCanvasES2_1.Height = 0) then Exit;
  66.   grScaleX := jCanvasES2_1.Width/jCanvasES2_1.Height;
  67.   grScaleX := 1;
  68. end;
  69.  
  70. end.

- If I remove grCalculateScaleValues (line #65, #66, #67), the crash won't happen.
- Or if I remove Screen_Clear (line #54), the crash won't happen too.

How to reproduce:
01. Download the code, compile and build the apk.
02. Copy the apk to your mobile phone and run it.
03. Click the "Start" button once.
04. Press the white rectangle repeatedly as fast as possible. I use both my thumbs.
05. It crashes with message "Unfortunately, MicroWorld has stopped".

The code compiled using Lazarus 1.8.0 64-bit LAMW 0.7.0.0 (svn 695). Tested on Redmi 4X Octa-core 1.40GHz Android 6.0.1.
Title: Re: A LAMW Bug?
Post by: Handoko on January 18, 2018, 06:34:41 pm
This is the apk for testing.
Title: Re: A LAMW Bug?
Post by: jmpessoa on January 27, 2018, 05:39:11 pm

Hello, Handoko!

Ok. I will try fix it!
Title: Re: A LAMW Bug?
Post by: jmpessoa on February 01, 2018, 04:04:14 am
Added methods:
   DispatchTouchDown(_value: boolean )
   DispatchTouchMove(_value: boolean )
   DispatchTouchUp(_value: boolean)

use:
Code: Pascal  [Select][+][-]
  1. procedure TAndroidModule1.AndroidModule1JNIPrompt(Sender: TObject);
  2. begin
  3.   jCanvasES2_1.DispatchTouchDown(False);
  4.   jCanvasES2_1.DispatchTouchMove(False);
  5.   jCanvasES2_1.DispatchTouchUp(False);
  6. end;
  7.  



Title: Re: A LAMW Bug?
Post by: Handoko on February 09, 2018, 03:32:08 pm
Failed to correctly install svn 696.

I manually downloaded svn 696 and installed as how I usually did (open and install: tfpandroidbridge_pack, lazandroidwizardpack, amw_ide_tools). All these 3 packages were able to be compiled and installed, rebuilding the IDE was okay too. But if I open my android project and build, I get error. See the image below.

If I install LAMW using Online Package Manager, everything seemed to be okay except the DispatchTouchDown/Move/Up commands aren't recognized. So I think the Online Package Manager still using svn 695 or below.
Title: Re: A LAMW Bug?
Post by: jmpessoa on February 10, 2018, 01:32:23 am

Quote
But if I open my android project and build, I get error

What about only open??  bug?
or the bug is on build?
Title: Re: A LAMW Bug?
Post by: Handoko on February 10, 2018, 03:33:57 am

Quote
But if I open my android project and build, I get error

What about only open??  bug?
or the bug is on build?

- Manually install svn 696, rebuild Lazarus IDE ---> no error
- Manually install svn 696, open my android project ---> no error
- Manually install svn 696, open non android project ---> no error
- Manually install svn 696, open my android project and build ---> get error
- Manually install svn 696, open non android project and build ---> no error
- Install LAMW using OPM, open my android project and build ---> no error
- Install LAMW using OPM, build DispatchTouchDown/Move/Up ---> fail

I tested several times, those above are the results. So I believe I managed to install LAMW svn 696 but it wasn't installed correctly. Maybe there is something wrong with my Lazarus, I haven't tested with clean installation.
Title: Re: A LAMW Bug?
Post by: Handoko on March 17, 2018, 06:02:48 am
I retested LAMW again, now with revision 697. But still no luck:

- Manually uninstalled all the previous LAMW from my computer
- Downloaded LAMW  revision 697 using RapidSVN
- Installed the packages: tfpandroidbridge_pack, lazandroidwizardpack, amw_ide_tools
- All the 3 packages were installed successfully without error
- I can open my LAMW projects without problem
- But compiling (Ctrl+F9) old projects, I got "List Index (4427627) out of bounds" error
- Same error if using build (Shift+F9)
- Same error when compiling AppDemo1 (demo supplied by LAMW)
- No problem when compiling non-Android projects
- Tested on Lazarus 1.8.0 64-bit Gtk2 FPC 3.0.4 Ubuntu Mate 17.10

But if compiling a new LAMW project (File > New > Android [GUI] JNI Module [Lamw]), it works.
Title: Re: A LAMW Bug?
Post by: A.S. on March 17, 2018, 02:45:23 pm
Cannot reproduce on Lazarus 1.8.2 64-bit Gtk2 FPC 3.0.4 Ubuntu 16.04.
It should be noticed, that for successful compilation of AppDemo1 it is needed to clear Custom options.
What android SDK do you use?
When you've got an exception is there any console output? (some kind of stacktrace would be appreciated)
Title: Re: A LAMW Bug?
Post by: Handoko on March 17, 2018, 06:43:11 pm
There were no any output on the Message Window. The IDE will exit immediately if I click 'Abort'. If I click 'Ok', the error warning will be close, still the same no any output on the Message Window.

The error shows up immediately after clicking the 'compile' button. This makes me think this is an issue on the IDE, not FPC because the 'real' compiling process hasn't started yet.

It should be noticed, that for successful compilation of AppDemo1 it is needed to clear Custom options.

I didn't update LAMW frequently. But previously compiling the AppDemo1 just worked without clearing custom options nor modify any settings.

Cannot reproduce on Lazarus 1.8.2 64-bit Gtk2 FPC 3.0.4 Ubuntu 16.04.

Thanks for the testing. I will perform more tests and report back with more info later. I'm thinking to try it on Lazarus 1.8.2.
Title: Re: A LAMW Bug?
Post by: A.S. on March 17, 2018, 07:48:22 pm
Try to start Lazarus IDE manually by "startlazarus" in console and look on the messages in this console when the error you mentioned arise.
Title: Re: A LAMW Bug?
Post by: Handoko on March 17, 2018, 07:54:30 pm
I did as what you said:

Quote
TApplication.HandleException List index (4427627) out of bounds
    Stack trace:
    $0000000000515DA2
Title: Re: A LAMW Bug?
Post by: A.S. on March 17, 2018, 09:42:56 pm
Try to rebuild Lazarus IDE with debug info (use "Debug IDE" profile in "Configure build Lazarus" dialog). Stacktrace should contain source file names and line numbers.
Title: Re: A LAMW Bug?
Post by: Handoko on March 18, 2018, 07:35:57 am
Here are the messages when compiling the AppDemo1:
Title: Re: A LAMW Bug?
Post by: A.S. on March 18, 2018, 10:13:36 am
Not sure, but probably your LAMW was compiled with different debug info. Open LazAndroidWizardPack package (menu Package->Open loaded package... and search for "LazAndroidWizardPack" then open it) and check that Custom options contain only "$(IDEBuildOptions)" (w/o quotes).
You can also try to rebuild IDE (with debug info) with "clean all" options (for the case if LAMW was not recompiled).
As far as I can see, lazideintf.pas:648 should call TLamwSmartDesigner.OnProjectSavingAll, so error may be somewhere in this function.
Title: Re: A LAMW Bug?
Post by: jmpessoa on March 18, 2018, 07:24:59 pm

In windows, if you try recompile/reinstall any LAMW package,  when a "LAMW" cross-project is open in IDE...  you can damage the IDE ....  so it is recommended that you perform these tasks first by opening a native windows project...
Title: Re: A LAMW Bug?
Post by: Handoko on March 18, 2018, 07:31:51 pm
Thank you. After several testings, I'm sure it is a bug how Lazarus handling package installation. I managed to solved the issue and reproduce it,  it seems to be something to do with the config files. I need more tests and will report back what I found. Maybe tomorrow, it's midnight here in my country.
Title: Re: A LAMW Bug?
Post by: Thaddy on March 18, 2018, 07:47:49 pm
That's not a Lazarus bug, but an LAMW bug. LAMW should conform to the way Lazarus handles package installation: decent proxies (separate design for real code) , do not touch config files unless necessary and tested, Use {$if defined(LAMW)} or something to prevent accidents like this.
Title: Re: A LAMW Bug?
Post by: Handoko on March 18, 2018, 07:55:32 pm
Please don't draw conclusion too early. Still need more testings. Be patient please.
Title: Re: A LAMW Bug?
Post by: Thaddy on March 18, 2018, 09:04:08 pm
I did actually test. Hence my conclusions. It is the way LAMW works which is atm. incompatible.
I did no extensive tests so miles may very, but the issues I mentioned are there. Note when compiling the code from the command line everything works fine if the paths are correct.
Title: Re: A LAMW Bug?
Post by: Handoko on March 19, 2018, 05:33:55 pm
Finally, I found the file and the exact line that causes the issue. It is in JNIAndroidProject.ini on the line of CanUpdateJavaTemplate=f

My LAMW worked but until it upgraded to revision 696, it got "List index out of bounds" error if I try to compile any of my LAMW projects. I tried to fix it by upgrading/downgrading to higher/lower revisions manually and OPM, and rebuilding the IDE. None of them can bring back my LAMW to work.

I had never manually edit the JNIAndroidProject.ini before, but I noticed the contents are very different if I delete the file and let LAMW to regenerate it. I keep a backup of the problem ini file. You can see img1 for the comparison.

After many tests, I found that it will work again if I simply delete the line:
CanUpdateJavaTemplate=f

img1.png is the comparison of the problem and 'regenerated' ini files using Meld. The left one is my original JNIAndroidProject.ini, which is problematic. The right one is the regenerated by LAMW.

img2.png is my settings for LAMW. For you information:
/home/handoko/Android/sdk is a link to /home/handoko/Android/android-sdk-linux
/home/handoko/Android/ndk is a link to /home/handoko/Android/android-ndk-r11c

JNIAndroidProject.ini.zip is the config file that causes the error, if anyone interested to test.
Title: Re: A LAMW Bug?
Post by: jmpessoa on March 20, 2018, 04:37:01 am
Quote
After many tests, I found that it will work again if I simply delete the line:
CanUpdateJavaTemplate=f

Thank you!!

NOTE: After removed,   "Can Update Java Template" will be always true (default) !!!
Title: Re: A LAMW Bug?
Post by: Handoko on March 20, 2018, 10:03:40 am
Thank you. Using DispatchTouchDown/Move/Up really can solve the crash issue but it also introduces new issue. Tested on LAMW revision 700 on Android 7.1.2 phone.

Below is the code I used for testing, which also provided in the attachements:
Code: Pascal  [Select][+][-]
  1. {Hint: save all files to location: /home/handoko/Desktop/MicroWorld/jni }
  2. unit unit1;
  3.  
  4. {$mode delphi}
  5.  
  6. interface
  7.  
  8. uses
  9.   Classes, SysUtils, AndroidWidget, Laz_And_Controls, Laz_And_GLESv2_Canvas,
  10.   Laz_And_GLESv2_Canvas_h;
  11.  
  12. type
  13.  
  14.   { TAndroidModule1 }
  15.  
  16.   TAndroidModule1 = class(jForm)
  17.     jButton1: jButton;
  18.     jCanvasES2_1: jCanvasES2;
  19.     jTextView1: jTextView;
  20.     jTimer1: jTimer;
  21.     procedure AndroidModule1Close(Sender: TObject);
  22.     procedure AndroidModule1JNIPrompt(Sender: TObject);
  23.     procedure jButton1Click(Sender: TObject);
  24.     procedure jCanvasES2_1GLDown(Sender: TObject; Touch: TMouch);
  25.     procedure jCanvasES2_1GLDraw(Sender: TObject);
  26.     procedure jTimer1Timer(Sender: TObject);
  27.   private
  28.     var
  29.       grScaleX, grScaleY: Single; // Used internally for my graphics engine
  30.     procedure grCalculateScaleValues;
  31.     procedure ShowInfo;
  32.   end;
  33.  
  34. var
  35.   AndroidModule1 : TAndroidModule1;
  36.   TouchCount     : Integer = 0;
  37.  
  38. implementation
  39.  
  40. {$R *.lfm}
  41.  
  42. { TAndroidModule1 }
  43.  
  44. procedure TAndroidModule1.AndroidModule1Close(Sender: TObject);
  45. begin
  46.   jTimer1.Enabled := False;
  47. end;
  48.  
  49. procedure TAndroidModule1.AndroidModule1JNIPrompt(Sender: TObject);
  50. begin
  51.   jCanvasES2_1.DispatchTouchDown(False);
  52.   jCanvasES2_1.DispatchTouchMove(False);
  53.   jCanvasES2_1.DispatchTouchUp(False);
  54. end;
  55.  
  56. procedure TAndroidModule1.jButton1Click(Sender: TObject);
  57. begin
  58.   jTimer1.Enabled := not(jTimer1.Enabled);
  59.   ShowInfo;
  60. end;
  61.  
  62. procedure TAndroidModule1.jCanvasES2_1GLDown(Sender: TObject; Touch: TMouch);
  63. begin
  64.   Inc(TouchCount);
  65.   ShowInfo;
  66. end;
  67.  
  68. procedure TAndroidModule1.jCanvasES2_1GLDraw(Sender: TObject);
  69. begin
  70.   jCanvasES2_1.Screen_Setup (jCanvasES2_1.Width, jCanvasES2_1.Height, xp2D,cCull_YES);
  71.   jCanvasES2_1.Screen_Clear(1,1,1,1);
  72.   grCalculateScaleValues;
  73. end;
  74.  
  75. procedure TAndroidModule1.jTimer1Timer(Sender: TObject);
  76. begin
  77.   jCanvasES2_1.Refresh;
  78. end;
  79.  
  80. procedure TAndroidModule1.grCalculateScaleValues;
  81. begin
  82.   if (jCanvasES2_1.Height = 0) then Exit;
  83.   grScaleX := jCanvasES2_1.Width/jCanvasES2_1.Height;
  84.   grScaleX := 1;
  85. end;
  86.  
  87. procedure TAndroidModule1.ShowInfo;
  88. var
  89.   S: string;
  90. begin
  91.   case jTimer1.Enabled of
  92.     True:  S := 'Timer is on.';
  93.     False: S := 'Timer is off.';
  94.   end;
  95.   jTextView1.Text := S + LineEnding + 'TouchCount = ' + TouchCount.ToString;
  96. end;
  97.  
  98. end.

If I disable line #51..#53, the app will crash if I touch the white box (see image below) multiple times repeatedly rapidly. The crash won't be happen if the Timer is off.

If those lines are enabled, the crash issue is gone but the TouchCount in jCanvasES2_1GLDown won't be increased, see line #64.

The image on the left side is the screenshot when those lines are enabled, the TouchCount always stays zero.
Title: Re: A LAMW Bug?
Post by: Handoko on March 20, 2018, 10:07:11 am
You can download this apk for testing. This one is without using DispatchTouch.
Title: Re: A LAMW Bug?
Post by: Handoko on March 20, 2018, 10:08:37 am
And this one is the apk using DispatchTouch.

Sorry I have to separate the attachments, due to the size limitation per post.
Title: Re: A LAMW Bug?
Post by: Seenkao on March 29, 2022, 05:27:30 pm
Я в вызываемых процедурах отключал/включал таймеры. Это нужно стало когда я привязывал ZenGL к LCL-окну. При работе приложения, если таймер включен, то в момент обработки процедуры, может запустится таймер и это вызовет сбой.
В ZenGL это 17-я демка. Посмотрите её, вполне возможно она вам поможет.

google translate:
I disabled/enabled timers in the called procedures. It became necessary when I attached ZenGL to the LCL window. When the application is running, if the timer is enabled, then at the time of processing the procedure, the timer may start and this will cause a crash.
This is the 17th demo in ZenGL. Check it out, it might help you.
Title: Re: A LAMW Bug?
Post by: Handoko on March 29, 2022, 05:39:13 pm
That sound possible. I will try it. Thank you again.
TinyPortal © 2005-2018