B4R Question Numeric string can cause memory loss

santook

Member
I found a problem,When we try to assign a numeric type to a string type,Just like the code snippet below.We will find that MCU will continue to repeat the reset.Because Stack Buffer is constantly occupied.
Is there an available Stack Buffer release method?
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.
    Public Serial1 As Serial
    Public i As ULong
   
End Sub

Private Sub AppStart
    Dim s As String
    Serial1.Initialize(115200)
    Log("AppStart")
    i=0
    Do While(True)
        i=i+1
        s=i
        Log("Temp:",s)
        Log("MEM:",AvailableRAM,"Stack Buf:",StackBufferUsage)
        Delay(50)
    Loop
End Sub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I've removed some posts as it was difficult to understand the question with all the posts.

There is no bug here.

Converting a number to string is done using the stack buffer. Every call will take a few bytes. The memory will be released once the current sub ends.
As a general rule you should avoid using strings. You can log the value directly as int. No reason to convert it to string.

Another option:
B4X:
Sub Sub1
 Do While (True)
  i = i + 1
  Sub2(i)
 Loop
End Sub

Sub Sub2 (i As Int)
  Dim s As String = i
  Log(s)
End Sub
 
Upvote 0

santook

Member
I need to export the data in JSON format to the server via MQTT,So the value is converted to a string, and then GetBytes more convenient.
 
Upvote 0
Top