B4R Question Strange crash after writing to file (WeMos)

Computersmith64

Well-Known Member
Licensed User
Longtime User
Hi,

I'm using the rESP8266FileSystem library to write to / read from a config file on my WeMos D1 R2's built-in flash file system.

When I first boot the board, I can write to the config file as many times as I want with no issues - however when I send config data from a B4A app via UDP, copy that config data into variables on the WeMos & then write it to the config, the board crashes with this error:

Exception (9):
epc1=0x402095a6 epc2=0x00000000 epc3=0x00000000 excvaddr=0x342e333a depc=0x00000000
ctx: cont
sp: 3ffef840 end: 3ffefa80 offset: 01a0
>>>stack>>>
3ffef9e0: 3ffee874 3fff1b4c 3fff103c 3fff0f14
3ffef9f0: 3fffdad0 3fff0354 3ffee874 40208b37
3ffefa00: 3ffe8700 00008bc1 3ffee7b0 40207355
3ffefa10: 00000000 000084e3 3ffee788 4020751a
3ffefa20: 3ffe8480 00000001 3ffee814 3ffeea58
3ffefa30: 3fffdad0 3fff0354 3ffee7a8 40207414
3ffefa40: 3fffdad0 3ffee7b0 3fff10b4 4020747b
3ffefa50: feefeffe 00000000 3ffeea50 40208cd3
3ffefa60: feefeffe feefeffe feefeffe 4020a4c4
3ffefa70: feefeffe feefeffe 3ffeea60 40100718
<<<stack<<<
ets Jan 8 2013,rst cause:2, boot mode:(1,6)
ets Jan 8 2013,rst cause:4, boot mode:(1,6)
wdt reset

The weird thing is that this crash happens after the file has been written & closed - so it's not an issue with the format of the data (it's the same variables that I'm writing every time anyway & even if I hard code data in there, I still get the crash).

I've done some searching online & it seems that the "Exception(9)" is something to do with the WiFi resetting (or something), but I haven't really been able to find anything to help me.

Here's the code I use to write to the config:

B4X:
Private Sub saveConfig
    Private buffer() As Byte
    Private bc As ByteConverter
   
    If fs.OpenReadWrite("myFuelConfig.dat") Then
        Log("Config open for writing")
        fs.Position = 0
        buffer = bc.StringToBytes(JoinStrings(Array As String(supplyRate, ":", returnRate, ":", calibrationOffset))) 'local config variables
        Log("BUFFER: ", buffer)
        fs.Stream.WriteBytes(buffer, 0, buffer.Length)
        fs.Stream.Flush
        fs.Close
        Log("Config Saved")
    End If
End Sub

This is driving me absolutely nuts. I can't get it to work no matter what I do.

- Colin.
 

Computersmith64

Well-Known Member
Licensed User
Longtime User
Avoid using strings and especially avoid using JoinStrings. This can easily lead to memory issues.
You should instead call fs.Stream.WriteBytes multiple times.

What is the type of supplyRate, returnRate and calibrationOffset?
Thanks Erel,

The types are:

B4X:
Public supplyRate As Double
Public returnRate As Double
Public calibrationOffset As Int

I was sending the items in a colon-separated list - eg: "3.46:2.55:6" & then running a ByteConverter Split iterator to separate them on the receiving end - however I have just rewritten the code so that I send each calibration item individually from the B4A client & then once they have all been received on the B4R end, I save the config. This seems to have resolved the issue without removing the JoinStrings.

From what I could find on the web, it seems that this issue can be caused by long-running loops, so the change I have made has eliminated the Split iterator loop - but I really can't see how a loop that separates 3 values could be "long-running"...

The code I was using to split the received values was:

B4X:
Public Sub calibrationUpdateReceived(msg As String)
    Private cal(4) As String
    Private i As Int = 0
    Private bc As ByteConverter
       
    For Each s() As Byte In bc.Split(msg, ":")
        cal(i) = bc.StringFromBytes(s)
        i = i + 1
    Next
    supplyRate = cal(1)
    returnRate = cal(2)
    calibrationOffset = cal(3)
    saveConfig
End Sub

& getting rid of this seems to have resolved the issue.

- Colin.
 
Upvote 0
Top