Looping problem

William Hunter

Active Member
Licensed User
Longtime User
I am having a few issues with a project. I need to resolve these issues one at a time. The code below consists of two subs. When the code in the second sub has finished executing, there is an unintended return to the first sub. The two subs behave as if they were intentionally looped, and will persist in executing over and over. :BangHead:

How can I stop a return to sub one, once sub two has finished executing? Help in this regard would be greatly appreciated.
B4X:
Sub pop_Download2Completed (Success As Boolean, MessageId As Int, Message As String, MainHeaders As Map)
   Dim arraySubject(messageCount) As String
   Dim arrayFrom(messageCount) As String
            
   delFlag = True
   SelectFlag = False
   
   aIndex = 0
   MessageId = 2
   Do While aIndex < messageCount
      arraySubject(aIndex) = (MainHeaders.Get("SUBJECT"))
      arrayFrom(aIndex) = (MainHeaders.Get("FROM"))
      If aIndex < messageCount - 1 Then ' don't execute next line if header downloads completed
         pop.DownloadMessage2(MessageId, 0, True, False)
      End If
      aIndex = aIndex + 1
      MessageId = MessageId + 1
   Loop
   pop.Close ' the connection will be closed
   'ToastMessageShow("Download2Completed", False) ' For testing only
   CreateScrollView
End Sub

Sub CreateScrollView
   '#####################################################################################
   '# ScrollView does not populate correctly. If say two items in array, the first item #
   '# is displayed in both checkboxes - then second item is displayed in both checkboxes#
   '# overwriting the previous first items displayed. Strange behaviour I can't resolve.#
   '# This loop seems to be persist in a random manner irrespective of messageCount ??? #                   #
   '#####################################################################################
   Dim ScrollView1 As ScrollView
   Dim lstChecks As List
   Dim height As Int : height = 40dip
   Dim Panel1 As Panel
      
   ScrollView1.Initialize(0)
   Panel1 = ScrollView1.Panel
   Activity.AddView(ScrollView1, 5, 25, 99%x, 81%y)
   lstChecks.Initialize
   aIndex = 0
   For i = 1 To messageCount
      'ToastMessageShow(aIndex & " Why does loop persist beyond once?", False) ' For testing only
      Dim chk As CheckBox
      chk.Initialize("chkBoxSelection")
      Subject = arraySubject(aIndex) 'get the item from the array
      chk.Text = Subject
      chk.TextSize = FontSize
      chk.TextColor = FontColor
      chk.tag = i
      lstChecks.Add(chk)
      Dim lbl1 As Label
      lbl1.Initialize("")
      From = arrayFrom(aIndex) 'get the item from the array
      lbl1.Text = From
      lbl1.Gravity = Gravity.CENTER_VERTICAL
      lbl1.TextSize = FontSize
      lbl1.TextColor = FontColor
      Panel1.AddView(chk, 0, height * (i - 1), 49%x, height)
      Panel1.AddView(lbl1, 50%x, height * (i - 1), 49%x, height)
      aIndex = aIndex + 1
   Next
   Panel1.height = lstChecks.Size * height
End Sub
 

William Hunter

Active Member
Licensed User
Longtime User
CreateScrollView will be called many times. You are then referencing the same global variable and creating chaos...

Are you trying to download a list of messages and show it in a scrollview?

Thank you for your reply - I know I'm creating chaos. I just don't know why, or how to overcome it. The first sub downloads header info from a mail server into two arrays. The second sub is intended to access those arrays, and then show that header info in a scrollview. It all seems to be a slam dunk in theory but, as you have said, chaos in practice. I'm lost on how to proceed with this.
 
Upvote 0

William Hunter

Active Member
Licensed User
Longtime User
In order to better understand the behavior of Download2Completed I recommend you to add a Log statement at the beginning of this sub and at the beginning of CreateScrollView.

Hello Erel – I have managed to stop the persistent looping by enclosing the code in Sub pop_Download2Completed within an If – End If, and then triggering the If by means of a Boolean flag, set at the end of Sub CreateScrollView. This ends the chaos, allowing me to view and copy the unfiltered log.

The unfiltered log seems to indicate that buffer size is at the root of the persistent looping between the two subs. If this were correct, how would this be resolved? This is my first foray into POP3 and B4a, so I’m learning as I go.

I believe the header info is being downloaded and stored efficiently, and that wPOP is being used as the author intended, but I won’t be able to confirm this until the persistent looping is resolved

The unfiltered log is included below. Do you have any further thoughts on this? I would greatly appreciate any help you can give.
B4X:
>>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
CheckJNI is ON

--- registering native functions ---
GC_EXPLICIT freed 130 objects / 6904 bytes in 176ms
Scanning package: /data/app/vmdl74445.tmp
Removing non-system package:mail.purge
Force stopping package mail.purge uid=10036
Scanning package mail.purge
Package mail.purge codePath changed from /data/app/mail.purge-1.apk to /data/app/mail.purge-2.apk; Retaining data and using new
/data/app/mail.purge-2.apk changed; unpacking
DexInv: --- BEGIN '/data/app/mail.purge-2.apk' ---
DexOpt: load 314ms, verify 1118ms, opt 37ms
DexInv: --- END '/data/app/mail.purge-2.apk' (success) ---
Code path for pkg : mail.purge changing from /data/app/mail.purge-1.apk to /data/app/mail.purge-2.apk
Resource path for pkg : mail.purge changing from /data/app/mail.purge-1.apk to /data/app/mail.purge-2.apk
  Activities: anywheresoftware.b4a.objects.preferenceactivity mail.purge.main
Force stopping package mail.purge uid=10036
move /data/dalvik-cache/data@[email protected]@classes.dex -> /data/dalvik-cache/data@[email protected]@classes.dex
New package installed in /data/app/mail.purge-2.apk
Force stopping package mail.purge uid=10036
GC_EXPLICIT freed 6993 objects / 454416 bytes in 391ms
GC_EXPLICIT freed 98 objects / 4408 bytes in 309ms
GC_EXPLICIT freed 3364 objects / 166128 bytes in 666ms
unlink /data/dalvik-cache/data@[email protected]@classes.dex
no available voice recognition services found
Shutting down VM
adbd disconnected

>>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
CheckJNI is ON
--- registering native functions ---
Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=mail.purge/.main }
Shutting down VM
adbd disconnected
NOTE: attach of thread 'Binder Thread #3' failed
Start proc mail.purge for activity mail.purge/.main: pid=1186 uid=10036 gids={3003}
** Activity (main) Create, isFirst = true **

GC_FOR_MALLOC freed 7719 objects / 334136 bytes in 154ms

** Activity (main) Resume **

Displayed activity mail.purge/.main: 3846 ms (total 3846 ms)

Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.

Default buffer size used in BufferedWriter constructor. It would be better to be explicit if an 8k-char buffer is required.

Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.

GC_EXPLICIT freed 47 objects / 2296 bytes in 154ms

Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.

GC_FOR_MALLOC freed 6725 objects / 482120 bytes in 233ms

Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.

Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.

Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.

Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.

Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.

GC_FOR_MALLOC freed 2918 objects / 558808 bytes in 127ms

Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.

Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.

Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.

Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.

Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.

GC_FOR_MALLOC freed 2713 objects / 526712 bytes in 113ms

Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.

GC_EXPLICIT freed 167 objects / 11832 bytes in 328ms

Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.

Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.

GC_EXPLICIT freed 771 objects / 42384 bytes in 198ms

No keyboard for id 0

Using default keymap: /system/usr/keychars/qwerty.kcm.bin

AndroidRuntime onExit calling exit(0)

Process mail.purge (pid 1186) has died.

WIN DEATH: Window{4502d660 mail.purge/mail.purge.main paused=false}
Unexpected resume of com.android.launcher while already resumed in mail.purge

Got RemoteException sending setActive(false) notification to pid 1186 uid 10036
 
Last edited:
Upvote 0

William Hunter

Active Member
Licensed User
Longtime User
View attachment 10925View attachment 10925
The buffer messages are not related.
Hello Erel – Thank you for your reply. I have setup log points at various points in my project, as you previously suggested. The filtered log appears below. The error was logged in Sub pop_StatusCompleted, and seems to be associated with wPOP. wPOP is an extended library authored by fellow forum member wl. I’m not sure of the significance of this error, and whether or not it’s the cause of the persistent looping.

As a side comment, any reference I have found referring to log points, states that the log can be viewed in the LogCat tab. I do not have a LogCat tab, but have found that I can view these logs with the filtered checkbox enabled under the log tab. So, I hope things have been done correctly.

I would very much like to know the cause of this persistent looping, and have uploaded my source code. I look forward to your reply. :BangHead:

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

** Activity (main) Resume **

main_pop_statuscompleted (java line: 1020)

java.lang.RuntimeException: Object should first be initialized (Exception).
   at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:45)
   at anywheresoftware.b4a.objects.B4AException.getMessage(B4AException.java:21)
   at mail.purge.main._pop_statuscompleted(main.java:1020)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:521)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:113)
   at anywheresoftware.b4a.BA$1.run(BA.java:218)
   at android.os.Handler.handleCallback(Handler.java:587)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:123)
   at android.app.ActivityThread.main(ActivityThread.java:4627)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:521)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
   at dalvik.system.NativeStart.main(Native Method)
java.lang.RuntimeException: Object should first be initialized (Exception).
 

Attachments

  • MailPurge.zip
    11.6 KB · Views: 274
Last edited:
Upvote 0

William Hunter

Active Member
Licensed User
Longtime User
This error means that you are trying to do something with LastException and LastException was not initialized. Probably because there was no error.

You can run in Debug mode to see the actual line that raises the error.

Hello Erel – Thank you for your assistance. While wPOP has taken me on a merry chase, I believe that I now have a handle on things. For anyone, who out of common interest is following this thread, here are some of my findings:

1: If wPOP is taken out of the mix, the persistent looping ceases. A scenario creating the ScrollView from a pre-configured array for testing purposes works flawlessly, and the execution of code stops where it should. I have no understanding of why the inclusion of wPOP had this effect.

2. Using wPOP, connecting to a pop3 server will allow you to download header info for the first message only. You cannot request a sequential download of all header info in one run, for placement into an array. This just doesn’t work. While every index in the array will contain header info, each index will only contain header info for message number one. I don’t see a practical way of using wPOP to purge junk mail at this time.

Creating and understanding the inner workings of extended libraries is beyond my limited abilities. An application to purge junk mail would be extremely useful. Unfortunately with the tools at hand, this poor boy isn’t up to the task.

Erel – Is there any possibility of a future version of the Net Library that would facilitate the creation of this type of application?
 
Upvote 0

William Hunter

Active Member
Licensed User
Longtime User
wPOP

Try to post this description in wPOP library thread. Maybe the library author can fix it.

Hello Erel - The author was made aware of these comments at the time of posting, and has received further information in a PM. Hopefully these problems can be rectified.

Regards
 
Upvote 0
Top