B4R Question App resseting - memory problem

demasi

Active Member
Licensed User
Longtime User
Hello,
I wrote a simple program to demonstrate what is happening. I know I'm doing something wrong, but Arduino´s memory management is a weird animal.
If I use a function in an infinite loop, my program crashes due to lack of memory.

this works ok:

B4X:
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
    Dim n As Int = 0
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    Do While True
        n=n+1
        Log(n)
    Loop
End Sub

Sub teste(nn) As Int 
    Return nn
End Sub

but this one crashes repeatedely. this is caused by the call to the function. I can´t understand why this causes memory use.

In an arduino uno, the count goes until 247 and goes back to Appstart.

B4X:
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
    Dim n As Int = 0
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    Do While True
        n=n+1
        Log(test(n))
    Loop
End Sub

Sub test(nn) As Int 
    Return nn
End Sub

any help?
Thank you all
 

demasi

Active Member
Licensed User
Longtime User
Sure. it was my fault. Thank you.

I´m building a program for create effects in a 10x10 led panel using 2812b led strip. I have a sub that converts line and column to led number, the problem was I haven´t declared the type of the arguments, and it was assumed string.
But wasn't supposed that all the variables created in a sub are destroyed when the sub ends?
So, why the stack memory is decreased each time that sub was called?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
But wasn't supposed that all the variables created in a sub are destroyed when the sub ends?
So, why the stack memory is decreased each time that sub was called?
The actual memory leak is here:
B4X:
Log(test(n))
It creates a string object every iteration and it happens in AppStart sub.
 
Upvote 0

miker2069

Active Member
Licensed User
Longtime User
The actual memory leak is here:
B4X:
Log(test(n))
It creates a string object every iteration and it happens in AppStart sub.

Sorry to revive this thread but I found this interesting in that the leak is subtle, I would have missed it also. Is the fix to first set it to a local variable then print out? something like
B4X:
dim tmpstr = ""

Do While True
     n=n+1
     tmpstr = n
     Log(tmpstr)
     'Log(test(n))
Loop

Also when the device isn't connected to B4R are those log functions disabled? I know in Arduino sketches, there was a trick to IFDEF out the serial.print lines when you were done debugging.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…