Bug? Memory Leak in wifiSocket.ConnectHost ?

JanG

Member
Licensed User
I try to check the connection, by connecting to a host by the function ConnectHost. But if I do so, the ESP8266 memory is getting smaller and smaller... Is this a memory leak in the implementation or my mistake? Do I do anything wrong?

Addition:
The "Stack" is the AvailableRAM. And after a while this available RAM is growing up. I don't know what happens, but maybe the sketch is running without a crash anyway.

B4X:
Private Sub isConnected() As Boolean
    Dim isCon As Boolean
  
    Dim wifiSocket As WiFiSocket
  
  
    isCon = wifi.IsConnected
    If Not(isCon) Then
        Return False  
    End If
  
    Delay(100)
    isCon = wifiSocket.ConnectHost("example.com",80)
    If isCon Then
        wifiSocket.Close
    End If
  
    wifiSocket = Null
  
    Return(isCon)  
End Sub

B4X:
AppStart
Connected 1, AP: 1, Stack: 43872
Connected 1, AP: 1, Stack: 43544
Connected 1, AP: 1, Stack: 43216
Connected 1, AP: 1, Stack: 42888
Connected 1, AP: 1, Stack: 42560
Connected 1, AP: 1, Stack: 42232
Connected 1, AP: 1, Stack: 43768
Connected 1, AP: 1, Stack: 43872
Connected 1, AP: 1, Stack: 43544
Connected 1, AP: 1, Stack: 43216
Connected 1, AP: 1, Stack: 42888
Connected 1, AP: 1, Stack: 42560
Connected 1, AP: 1, Stack: 42232
Connected 1, AP: 1, Stack: 41904
Connected 1, AP: 1, Stack: 41576
Connected 1, AP: 1, Stack: 41248
Connected 1, AP: 1, Stack: 40920
Connected 1, AP: 1, Stack: 40592
Connected 1, AP: 1, Stack: 40264
Connected 1, AP: 1, Stack: 39936
Connected 1, AP: 1, Stack: 39608
Connected 1, AP: 1, Stack: 39280
Connected 1, AP: 1, Stack: 38952
Connected 1, AP: 1, Stack: 38624
etc...
 
Last edited:

JanG

Member
Licensed User
That was my first try, taking a global and reuse it (like the given examples). But the same memory consuming problem. As I said, after a while the free memory is growing up. But now I "fire" every 200 msec a request to the Wemos and... CRASH.

[EDIT]
Maybe a big issue... When connecting only once to Wemos and sending bytes to it, after that the free memory is lower. It's repeatable. Is this an issue from the hardware or from your generated C-code?
 
Last edited:

JanG

Member
Licensed User
Sorry for my late answer, I was on holidays...

I identified the problem a bit more specifically: When I leave out the codeline "CallSubPlus("CloseConnection", 200, 0)" (like in your samples) everything works fine, no memory consumption. But if I uncomment this line again and activate the code, every new data leads to less available RAM.

[EDIT]
The problem is when receiving multiple data serially. When only receiving one block of data and give enough time, the RAM will be available again after some minutes. But if you receive more data in between this minutes then we have the problem.
[/EDIT]

Here my example:
a) eating RAM
B4X:
#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 300
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.

    Private Serial1 As Serial
    Private wifi As ESP8266WiFi
    Private wifiServerSocket As WiFiServerSocket
    Private streamData As AsyncStreams
    Private timerCheck As Timer

    Private intervalTimer As Int = 1000
    Private conSsid As String = "<SSID>"
    Private conPassword As String = "<Password>"
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Delay(1000)
    Log("AppStart")

    timerCheck.Initialize("timerCheck_Tick", intervalTimer)
    timerCheck.Enabled = True
End Sub

Private Sub Connect
    Log("Connect")

    If wifi.Connect2(conSsid,conPassword) Then
        ListenServer
    End If
End Sub

private Sub ReConnect
    Log("ReConnect")

    If Not(wifi.IsConnected) Then
        Connect
    End If
End Sub

private Sub ListenServer
    Log("ListenServer")

    wifiServerSocket.Initialize(80, "wifiServerSocket_NewConnection")
    wifiServerSocket.Listen
End Sub

private Sub wifiServerSocket_NewConnection(newSocket As WiFiSocket)
    Log("wifiServerSocket_NewConnection")

    streamData.Initialize(newSocket.Stream, "streamData_NewData", "streamData_Error")
End Sub

private Sub CloseConnection(tag As Byte)
    Log("CloseConnection")

    If wifiServerSocket.Socket.Connected Then
        wifiServerSocket.Socket.Stream.Flush
        wifiServerSocket.Socket.Close
        Log("Connection closed")
    End If
End Sub

private Sub streamData_NewData(streamBuffer() As Byte)
    Log("streamData_NewData")

    CallSubPlus("CloseConnection", 200, 0)
End Sub

private Sub streamData_Error
    Log("streamData_Error")

    wifiServerSocket.Listen
End Sub

Sub timerCheck_Tick
    Log("Connected ", wifi.IsConnected, ", AvailableRAM: ", AvailableRAM)

    ReConnect
End Sub

a) non-eating RAM
B4X:
[...]
private Sub streamData_NewData(streamBuffer() As Byte)
    Log("streamData_NewData")

' this line is comment out for stop eating RAM
'    CallSubPlus("CloseConnection", 200, 0)
End Sub
[...]
 
Last edited:
Top