Occasional illegalState error

mc73

Well-Known Member
Licensed User
Longtime User
Dear friends,

occasionally I receive an illegalState error, while I'm trying to send data over the local network to another device. I use sockets and asynchronus streams, and just because the error is not frequent, I cannot easily debug it.
Here's what the log gives:
B4X:
serverlistener_astreams2_newdata (B4A line: 142)
aStreams(selectedSocket).Write(buffer)

java.lang.IllegalStateException: View com.android.internal.policy.impl.PhoneWindow$DecorView@4826adf0 has already been added to the window manager.
   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:125)
   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
   at android.view.Window$LocalWindowManager.addView(Window.java:424)
   at android.app.Dialog.show(Dialog.java:241)
   at anywheresoftware.b4a.debug.Debug.wait(Debug.java:202)
   at anywheresoftware.b4a.debug.Debug.reachBP(Debug.java:252)
   at anywheresoftware.b4a.debug.Debug.ErrorCaught(Debug.java:141)
   at wos.androidserver.serverlistener._astreams2_newdata(serverlistener.java:324)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:521)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:136)
   at anywheresoftware.b4a.BA$2.run(BA.java:244)
   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:858)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
   at dalvik.system.NativeStart.main(Native Method)

The routine in which this error seems to be cause is:
B4X:
Sub sendDataToClient(returningString As String,selectedSocket As Int)
    Dim myData As String 
    myData=returningString
   
    If myData.Length > 0 Then
        Dim buffer() As Byte
        buffer = myData.GetBytes("UTF8")
        aStreams(selectedSocket).Write(buffer)
    End If
End Sub

From an earlier discussion with Erel, I create individual sockets and aStreams for each connected device to my 'server' over which the above procedure runs. Anything wrong that I don't see in my code (as usual) ? Thank you.

PS: as already been said, I use individual sockets and streams, even though I'm not 100% sure I do it correctly.
Here's how I denote a new socket:
B4X:
Sub Server_NewConnection (Successful As Boolean, NewSocket As Socket)
    If Successful Then
    
        Dim freeSocketId As Int 
      freeSocketId=-1
      Dim k As Int 
      k=-1
      Do While freeSocketId=-1 AND k<9
      k=k+1
      If socketsStatus(k)=0 Then
      socketsStatus(k)=1:freeSocketId=k
      socket1(freeSocketId)=NewSocket
      fSocket=freeSocketId
      End If
      Loop
      
      If freeSocketId=-1 Then
      freeSocketId=0
      socket1(freeSocketId)=NewSocket
      fSocket=freeSocketId
      End If
      aStreams(freeSocketId).InitializePrefix(socket1(freeSocketId).InputStream, False, socket1(freeSocketId).OutputStream, "AStreams" & freeSocketId)
    
   Else
        ToastMessageShow(LastException.Message, True)
    End If
    server.Listen
end sub


Then, my astreams are of the form
B4X:
Sub AStreams0_NewData (Buffer() As Byte)
Dim currentSocket As Int 
currentSocket=0
msg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
End Sub
that is, I have astreams0, astreams1 and so on.

From the 'client' side, when reading is done, I am sending a "closeSocket" string-command, and so my 'server' is closing the corresponding open socket and stream.
 
Last edited:

mc73

Well-Known Member
Licensed User
Longtime User
Try running without the debugger. An error is raised and for some reason the debugger fails to show the error dialog. So the error message you see is actually related to the debugger.

Thank you for your reply, Erel. Actually, this app is running for two-three months now with this error being raised not very often. Here's what I've found so far:
when, for example, I have two different sockets and two astreams, in one activity, and they are both working almost at the same time, here's what happens with the server:
receives a newSocket event, it creates a socket numbered 0. Then, for the second connection, it creates a socket numbered 1. That's ok, of course. BUT, then, when I'm sending a 'closeSocket' string from the two sockets, in order to close them from the server-side, server receives BOTH string-commands as arriving from socket 1!
I got rid of this problem, by simply making a serial execution of the two sub-routines which use these sockets. BUT, I suspect a similar behavior from two-three different sockets created by other clients and this is my concern now. I mean, I'm sending data to socket 0,1,2,3,... but then sometimes (not frequently) server responds to just socket 1,2,3 overriding for example socket 0. This socket is then open, and it raises an error event when closed by client. Any idea what I'm doing wrong here?
 
Last edited:
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
First make sure that you do not run Debug app in production (the error you got is in the debugger code).

I guess that the problem you encounter is due to a usage of an incorrect index.
some of my devices get 'frozen' while net-operations are in progress, time to time, not very frequently. The index, if you mean the open socket, is checked so far, I didn't find unfortunately any bad typing there
One possible problem is the time it takes for my sqLite queries to be perfomed on the server side, while the server is receiving and try to proccess data in the same database, from another client. Now, I know it's very hard to suggest solutions without the source code, I will try to build a small project demonstrating what is going on. I'm sure that I am missing something upon the way sockets work. For example, that far, I am closing a socket, while an activity goes to pause state, while this was not my intention. I wanted it closed at the moment that the proccess performed was completed. I will try to get back with an example
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
I've just created a new thread about a latency in showing progressDialogBox, which I suspect is creating a new socket during a running operation. I truly hope this was the cause of my problem, I would be happy with your response on
this thread
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…