demasi Active Member Licensed User Longtime User Aug 30, 2017 #1 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: Code: 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. Code: 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
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: Code: 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. Code: 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
Erel B4X founder Staff member Licensed User Longtime User Aug 31, 2017 #2 The IDE shows a warning on this line: Sub test(nn) As Int Read the warning. It will explain the issue. Upvote 0
The IDE shows a warning on this line: Sub test(nn) As Int Read the warning. It will explain the issue.
demasi Active Member Licensed User Longtime User Aug 31, 2017 #3 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
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?
Erel B4X founder Staff member Licensed User Longtime User Aug 31, 2017 #4 demasi said: 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? Click to expand... The actual memory leak is here: Code: Log(test(n)) It creates a string object every iteration and it happens in AppStart sub. Upvote 0
demasi said: 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? Click to expand... The actual memory leak is here: Code: Log(test(n)) It creates a string object every iteration and it happens in AppStart sub.
M miker2069 Active Member Licensed User Longtime User Sep 28, 2017 #5 Erel said: The actual memory leak is here: Code: Log(test(n)) It creates a string object every iteration and it happens in AppStart sub. Click to expand... 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 Code: 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
Erel said: The actual memory leak is here: Code: Log(test(n)) It creates a string object every iteration and it happens in AppStart sub. Click to expand... 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 Code: 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.
Erel B4X founder Staff member Licensed User Longtime User Sep 29, 2017 #6 You should avoid using strings. The error in this thread happened because the type was missing and therefore string was used. The compiler shows a warning about this. Upvote 0
You should avoid using strings. The error in this thread happened because the type was missing and therefore string was used. The compiler shows a warning about this.