Hi,
In my program there is the variable Position* bestCellPos
.
In the following code snippet, I just print this to the console twice, but the 2nd time it prints something different:
toString()
looks like this:
String toString() const {
return String("{") + String(x) + String(" | ") + String(y) + String("}");
}
x
and y
are both integers.
Anyone know what can cause this behavior and how to fix it?
Sounds like memory corruption. Incorrect use of String (capital S) objects comes to mind. Note that in the case of memory corruption the error might be elsewhere in your code.
Which board?
Can you post the full code code or a representative example that we can use to reproduce.
Thanks for the fast response. Here is the whole source code . You have to import ArxContainer from the arduino library manager.
I use the Arduino Mega
horace
October 30, 2022, 5:30pm
4
your MapAnalyser.cpp compiles with multiple warnings, e.g.
MapAnalyzer.cpp:164:22: warning: 'vertices' may be used uninitialized in this function [-Wmaybe-uninitialized]
vertices->push_back(*bestCellPos);
start by fixing any problems
Yeah no , thassa lotta code to comb through.
How did you change it? You broke it maybe?
Didn't change it, you assert there is a problem, i think she who wrote it should hear about that.
a7
horace
October 30, 2022, 6:19pm
6
if I change the IntVector troString() to
String toString() const {
Serial.println(x);
Serial.println(y);
String s= String("{") + String(x) + String(" | ") + String(y) + String("}");
Serial.println("test " + s);
Serial.println(x);
Serial.println(y);
return s;
}
it now prints
breaked
1
0
test {1 | 0}
7
7
{1 | 0}
7
7
test {7 | 7}
7
7
{7 | 7}
7
7
test {7 | 7}
7
7
looks like String is corrupting memory???
horace
October 30, 2022, 7:53pm
7
consider this version of toString() which removes the multiple String() calls
String toString() const {
char text[50]={0};
Serial.println(x);
Serial.println(y);
sprintf(text, "{%d | %d}", x,y);
Serial.print("test ");
Serial.println(text);
Serial.println(x);
Serial.println(y);
return text;
}
run now gives
breaked
1
0
test {1 | 0}
1
0
{1 | 0}
7
7
test {7 | 7}
7
7
{7 | 7}
7
7
same problem - as soon as String() is used to return the method result corruption occures??
I wouls assume small microcontrollers such as the Mega are not suitable for programs constructing and destructing multiple objects
TimeLex
November 1, 2022, 10:34am
8
Yes, seems like the Mega can't handle String()
. I've now solved it by removing the toString()
function and creating following instead:
void print() const {
Serial.print("{");
Serial.print(x);
Serial.print(" | ");
Serial.print(y);
Serial.print("}");
}
void println() const {
print();
Serial.println();
}
It works now without issues.
system
Closed
April 30, 2023, 10:35am
9
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.