Android Question strange behavior in release mode

markcasio

Member
Licensed User
Longtime User
Hi, I have a question , what can cause a local variable in a sub to change it's value randomly.
The local var is not double used as a global var, and is only used in sub part1.

B4X:
sub part1
Dim k1,k2,k3,k6,k7,k8,k9,k10,k11,k12,k13 As Int '<=== local vars
k12 = 1234
 Log("k12 ="&k12) '<===== always gives 1234
anothersub '<== calling another sub
 Log("k12 ="&k12) '<===== always 1234 in debug but in release mode i somethimes get 357302632
end sub

What can cause a local variable to change it's value only in release mode.?
 

josejad

Expert
Licensed User
Longtime User
What if you set #BridgeLogger: True and Log(k12) in "anothersub" in release mode?
 
Upvote 0

markcasio

Member
Licensed User
Longtime User
Thank you for your reply,
Log(k12) in anothersub cannot be added because k12 is not declared in that sub, it really is a local variable.
So to test i declared k12 in anothersub as a local var and logged it as well.
K12 kept it's value in anothersub , but in the mainsub it is sometimes changing to a high random value after returning from anothersub in release mode.
i'm connected to the device using adb, i can see the logs in release mode.
If i rename k12 the problem jumps to other local variables and other subs, always after returning form a sub.
The random value is always something like : 357041136, 355992504 ....
 
Upvote 0

markcasio

Member
Licensed User
Longtime User
@LucaMs , did that k12 is only used in the main sub, if you rename k12 some other local var will be affected.
Just tested the same routine on other hardware with android 7.1.2 , no problems in release mode.
@Sandman if i scale down the application the problem will be gone or move to a new location, but there is no fixed point or routine that is responsible. Some local variables receive new values when returning from a sub,on a 8.1.0 machine. When i change a sub or rename k12 it will chose another var and sub.
I also tested other hardware same brand and also android version 8.1.0 , same problem.
I never saw anything like this, that is why i'm searching after the cause to prevent future problems.
Between the sub routines something has the power to change local variables.
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Do you use Resumable Sub?

Is it a variable that is tied to some runtime permission?
 
Upvote 0

markcasio

Member
Licensed User
Longtime User
Yes, there is a sleep command in a sub , i disabled that sub with the same results.
The sub was used to set a gpio pin high sleep(resume) and set the pin back to low.(end) with no other declarations.
No local affected variables are tied to runtime permissions.
 
Upvote 0

markcasio

Member
Licensed User
Longtime User
Found something, but can someone explain what i'm doing wrong , why was the string to int convertion unstable in release mode and works in debug ?
I have to call the main1sub in release mode few 100 times to get the wrong value .
The rate of the problem is also related to the time anothersub takes to return to main1.


B4X:
Sub main1

    Dim k11,k12,k13,k14,k15,k16,k17,k18,k19,k20 As Int '<== problem
    'Dim k11,k12,k13,k14,k15,k16,k17,k18,k19,k20 As String  '<== stable 
    
    Dim g As String

    g = "01000003010010110001" 'fixed string value always stable.
    
    'local declaration of k variables converted to int from num. parts of the string
    k11 = g.SubString2(2,4)'
    k12 = g.SubString2(4,5) 
    k13 = g.SubString2(5,6)'  
    k14 = g.SubString2(6,7)'  
    k15 = g.SubString2(7,8)'  
    k16 = g.SubString2(10,11)'
    k17 = g.SubString2(9,10)'  
    k18 = g.SubString2(14,15)'  
    k19 = g.SubString2(14,16) '
    k20 = g.SubString2(13,14) '  

    Log(k12) ' result always 0'
  
    Anothersub(k12)

    Log(k12) ' not always 0 value changes in release mode to high random value when i use integers.   
End Sub
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
My over half a century in writing software tells me that failure to resolve these non-obvious problems is often the result of invalid and often unconscious assumptions. That may or may not be the case here as all we have seen is the code for a single Sub, out of context. For example one assumption here is that the value of k12 is changed. It may be that k12 is fine but that the Log() is failing. I'm not saying it is - I'm merely pointing out what could be an invalid assumption. Without seeing the effect in an actual program we are all guessing but when we see 'impossible' things happening it is usually because we are looking in the wrong place!
 
Upvote 0

markcasio

Member
Licensed User
Longtime User
Thank you all for your time and feedback,

I have uploaded a video so you can see the problem,
var problem video

I first compile in debug mode, call the sub 100 times without a problem. Then i compile in release mode you will see the result in the log window.
The problem seems to be related to the android version 8.1.0 and only an integer local variable that has his value from a string conversion is affected.
If i execute the same code on a android 7.1.2 or lower i don't have this problem.

Maybe i have a configuration problem that makes the apk incompatible?
Current config settings:
C:\Program Files\Java\jdk-14.0.1\bin\javac.exe
C:\android-sdk\platforms\android-28\android.jar
 
Upvote 0

markcasio

Member
Licensed User
Longtime User
@Sandman i know , I just wanted to show and prove the existence of the problem.
And because the problem is android version related a shared demonstration will work fine on many other android hardware.
But as soon as i can narrow down the application to only the problem i will be happy to share it.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
You are using Java 14? The provided download for B4a is Java11. Have you tried it with Java11 from the download page? just in case.
 
Upvote 0

MikeSW17

Active Member
Licensed User
Longtime User
In your Video, it appeared to fail in the first iteration? Seems improbable for something that 'randomly' happens once in a 100 or so iterations?
I feel we are not getting the full explanation of the problem, and not just because of the lack of any uploaded code.... is that because it cannot be reproduced except in the full project?
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…