Android Code Snippet Close the socket and stream properly.

This approach prevents another process from attempting to close the socket and the stream at the same time.

B4X:
Private myclient As Socket
Private astream As AsyncStreams
Private IsClosing As Boolean

Public Sub CloseConnection
    If IsClosing Then Return
    IsClosing = True
    If astream.IsInitialized Then astream.Close
    If myclient.IsInitialized Then myclient.Close
    IsClosing = False
End Sub
 

Alessandro71

Well-Known Member
Licensed User
Longtime User
what is the use case: is there any chance of parallel processing in B4A?
i always thought that on Android the process was only one, with no resource contemption occourring.
or were you referring to a server implementation, or using the threading library?
 

Filippo

Expert
Licensed User
Longtime User
what is the use case: is there any chance of parallel processing in B4A?
The AsyncStreams library is asynchronous. This means that the Close command is not executed immediately, so problems can arise if you try to execute the “Close” command from another procedure.

I received a crash report from an app and asked an AI where this might be coming from.

Here is an excerpt from the “crash report”:
Date/Time: 2026-05-10 21:46:29.268 +0300
OS Version: iOS 26.4.2 (23E261)
Report Version: 104

Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0x000000000057e034
Triggered by Thread: 0

Thread 0 Crashed:
0 libobjc.A.dylib 0x0000000191f4db3c 0x191f4c000 + 6972 (objc_release_x0 + 8)
1 MoR 0x00000001042aa3a8 0x104088000 + 2237352 (-[B4IAsyncStreams Close] + 36)
2 MoR 0x00000001040e48a8 0x104088000 + 379048 (-[b4i_clsservermanager _closeconnection] + 136)
3 MoR 0x00000001040e4940 0x104088000 + 379200 (-[b4i_clsservermanager _closeexistingconnection:] +

My previous code looked like this:
B4X:
Public Sub CloseConnection
    If myclient.IsInitialized Then myclient.Close
    If astream.IsInitialized Then astream.Close
End Sub
 
Top