Problem with java.lang.StackOverflowError

knutf

Active Member
Licensed User
Longtime User
Hi all!

My code causes java.lang.StackOverflowError. The code consists of many subs and classes so I think at first I'm asking for help to interpret the log. See copy of the log below.

I think that the stack is a memory area. When a sub is invoked the arguments is placed at the top of the stack. If a new sub is started by the first sub, the arguments is laid on top of the arguments for the calling sub. When a sub is finished the arguments to the sub are removed from the stack and memory is released. Is this correct? Is it other things that can be stored on the stack?

The reason Stack Overflow error may then be:
1) The subs are too deeply nested. a sub starting another sub starting another sub etc etc
2) A sub calls itself multiple times

Could there be other reasons for Stack Overflow Error?

After the line "java.lang.StackOverflowError" is an amount of lines beginning with "at". What does the part after the "at" mean?
Is it references to sub-calls? I can not relate it to the code I created. At the end of the line is a number, is it a line number in the code?

Thanks Knut

B4X:
** Activity (main) Create, isFirst = true **


** Activity (main) Resume **


startService: class knutf.pumpestyring.servicepumpe


** Service (servicepumpe) Create **


** Service (servicepumpe) Start **
servicepumpe$_startwritevaluetom3argumentstoString (B4A line: 173)


TimerTimeoutCom.Enabled = True
java.lang.StackOverflowError


   at java.lang.String._getChars(String.java:1040)
   at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:155)
   at java.lang.IntegralToString.convertInt(IntegralToString.java:202)
   at java.lang.IntegralToString.appendInt(IntegralToString.java:166)
   at java.lang.StringBuilder.append(StringBuilder.java:140)
   at anywheresoftware.b4a.objects.ViewWrapper.toString(ViewWrapper.java:277)
   at anywheresoftware.b4a.objects.TextViewWrapper.toString(TextViewWrapper.java:69)
   at java.lang.String.valueOf(String.java:1860)
   at anywheresoftware.b4a.BA.TypeToString(BA.java:583)
   at anywheresoftware.b4a.B4AClass$ImplB4AClass.toString(B4AClass.java:30)
   at java.lang.StringBuilder.append(StringBuilder.java:203)
   at java.util.AbstractCollection.toString(AbstractCollection.java:384)
   at anywheresoftware.b4a.AbsObjectWrapper.toString(AbsObjectWrapper.java:110)
   at java.lang.String.valueOf(String.java:1860)
   at anywheresoftware.b4a.BA.TypeToString(BA.java:583)
   at anywheresoftware.b4a.B4AClass$ImplB4AClass.toString(B4AClass.java:30)
   at java.lang.String.valueOf(String.java:1860)
   at anywheresoftware.b4a.BA.TypeToString(BA.java:583)
   at anywheresoftware.b4a.B4AClass$ImplB4AClass.toString(B4AClass.java:30)
   at java.lang.StringBuilder.append(StringBuilder.java:203)
   at java.util.AbstractCollection.toString(AbstractCollection.java:384)
   at anywheresoftware.b4a.AbsObjectWrapper.toString(AbsObjectWrapper.java:110)
   at java.lang.String.valueOf(String.java:1860)
   at anywheresoftware.b4a.BA.TypeToString(BA.java:583)
   at anywheresoftware.b4a.B4AClass$ImplB4AClass.toString(B4AClass.java:30)
   at java.lang.String.valueOf(String.java:1860)
   at anywheresoftware.b4a.BA.TypeToString(BA.java:583)
   at anywheresoftware.b4a.B4AClass$ImplB4AClass.toString(B4AClass.java:30)
   at java.lang.StringBuilder.append(StringBuilder.java:203)
   at java.util.AbstractCollection.toString(AbstractCollection.java:384)
   at anywheresoftware.b4a.AbsObjectWrapper.toString(AbsObjectWrapper.java:110)
   at java.lang.String.valueOf(String.java:1860)
   at anywheresoftware.b4a.BA.TypeToString(BA.java:583)
   at anywheresoftware.b4a.B4AClass$ImplB4AClass.toString(B4AClass.java:30)
   at java.lang.String.valueOf(String.java:1860)
   at anywheresoftware.b4a.BA.TypeToString(BA.java:583)
   at anywheresoftware.b4a.B4AClass$ImplB4AClass.toString(B4AClass.java:30)
   at java.lang.StringBuilder.append(StringBuilder.java:203)
   at java.util.AbstractCollection.toString(AbstractCollection.java:384)
   at anywheresoftware.b4a.AbsObjectWrapper.toString(AbsObjectWrapper.java:110)
   at java.lang.String.valueOf(String.java:1860)
   at anywheresoftware.b4a.BA.TypeToString(BA.java:583)
   at anywheresoftware.b4a.B4AClass$ImplB4AClass.toString(B4AClass.java:30)
   at java.lang.String.valueOf(String.java:1860)
   at anywheresoftware.b4a.BA.TypeToString(BA.java:583)
   at anywheresoftware.b4a.B4AClass$ImplB4AClass.toString(B4AClass.java:30)
   at java.lang.StringBuilder.append(StringBuilder.java:203)
   at java.util.AbstractCollection.toString(AbstractCollection.java:384)
   at anywheresoftware.b4a.AbsObjectWrapper.toString(AbsObjectWrapper.java:110)
   at java.lang.String.valueOf(String.java:1860)
   at anywheresoftware.b4a.BA.TypeToString(BA.java:583)
   at anywheresoftware.b4a.B4AClass$ImplB4AClass.toString(B4AClass.java:30)
   at java.lang.String.valueOf(String.java:1860)
   at anywheresoftware.b4a.BA.TypeToString(BA.java:583)
   at anywheresoftware.b4a.B4AClass$ImplB4AClass.toString(B4AClass.java:30)
   at java.lang.StringBuilder.append(StringBuilder.java:203)
   at java.util.AbstractCollection.toString(AbstractCollection.java:384)
   at anywheresoftware.b4a.AbsObjectWrapper.toString(AbsObjectWrapper.java:110)
   at java.lang.String.valueOf(String.java:1860)
   at anywheresoftware.b4a.BA.TypeToString(BA.java:583)
   at anywheresoftware.b4a.B4AClass$ImplB4AClass.toString(B4AClass.java:30)
   at java.lang.String.valueOf(String.java:1860)
   at anywheresoftware.b4a.BA.TypeToString(BA.java:583)
   at anywheresoftware.b4a.B4AClass$ImplB4AClass.toString(B4AClass.java:30)


   at java.lang.StringBuilder.append(Str
 

sorex

Expert
Licensed User
Longtime User
You're right, the stack is used to hold references when calling sub routines so that it knows where to return when it hits an end sub or ret/rts.

In assembler you could set the stack size so that you could tweak the exe size a bit, but I guess it's done automatically these days.

Most common cause is an endless loop somewhere calling the same sub from in the sub, recursive sub'ing so to say.

Hard to tell without seeing any code tho.
 
Upvote 0

knutf

Active Member
Licensed User
Longtime User
Thanks for quick reply!

Project attached.

To induce the error in the app:
* Change to the PLC-tab
* write a number in one off the textedit
* Tap "Done"

Knutf
 

Attachments

  • PLCCom.zip
    19.6 KB · Views: 334
Upvote 0

sorex

Expert
Licensed User
Longtime User
ok, figured it out for you.

you forgot a tiny thing :)

add this to you main activity_create subroutine

TimeOutCom=100

it probably returned 0 since it didn't exist so the timer tick that invokes that plcread routine again probably never ended completely while it started again due to the timer.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
I guess that refers to a 0 milisecond timer definition?

he used

TimerTimeoutCom.Initialize("TimerTimeoutCom",Main.TimeOutCom)

but that variable didn't exist
 
Upvote 0

knutf

Active Member
Licensed User
Longtime User
Thank you Sorex, I have now done some testing, and found that you have right, it's the Timer called TimerTimeoutCom that caused the StackOverflowError. It was initialized with an interval=0.

I'v now learned a lot about the Stack in Java-Dalvik VM, and think I post a new thread about that later.
 
Upvote 0
Top