OutOfMemory exception
dev/null
Posts: 381
Can anyone tell me why I get an out of memory exception from this code?
(if I remove the calls to the subroutines I still get an exception, so there must be something with the UART....)
// Main loop
while (true) {
CPU.delay(10);
if (stampRX.byteAvailable()) {
c1 = (char)stampRX.receiveByte();
if (c1>'0') {
c2 = (char)stampRX.receiveByte();
switch(c1) {
case 'a': // Get angle and return to stamp
stampTX.sendString(padInt(getAngle(),3));
break;
case 'p': // Get angle and return to stamp
stampTX.sendString(getPIRStatus());
break;
case 't': // Get timer 1
stampTX.sendString(padInt(getTimerTick(c2),5));
break;
case 's':
stampTX.sendString(padInt(myGps.getSats(),3));
}
}
}
}
(if I remove the calls to the subroutines I still get an exception, so there must be something with the UART....)
// Main loop
while (true) {
CPU.delay(10);
if (stampRX.byteAvailable()) {
c1 = (char)stampRX.receiveByte();
if (c1>'0') {
c2 = (char)stampRX.receiveByte();
switch(c1) {
case 'a': // Get angle and return to stamp
stampTX.sendString(padInt(getAngle(),3));
break;
case 'p': // Get angle and return to stamp
stampTX.sendString(getPIRStatus());
break;
case 't': // Get timer 1
stampTX.sendString(padInt(getTimerTick(c2),5));
break;
case 's':
stampTX.sendString(padInt(myGps.getSats(),3));
}
}
}
}
Comments
The javelin does not have garbage collection and therefore temporary
strings must be avoided. Better to use StringBuffer which can be reused
and easily converted to type string.
regards peter
static String padInt(int inData, int inLength) {
String str = Integer.toString(inData);
int strLength = str.length();
for (int i=strLength;i<inLength;i++) str = "0" + str;
return str;
}
char[noparse]/noparse buf = new char[noparse][[/noparse]7];
int value;
Format.sprintf(buf,"%05d",value);
creates a null terminated decimal string with padded zeroes.
If you declare buf globally, you can call sprintf() inline so
no need for a seperate method.
regards peter