Shop OBEX P1 Docs P2 Docs Learn Events
OutOfMemory exception — Parallax Forums

OutOfMemory exception

dev/nulldev/null Posts: 381
edited 2009-06-30 20:08 in General Discussion
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));

}
}
}
}

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2009-06-30 17:51
    Does your PadInt() creates a temporary string each time it is called?
    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
  • dev/nulldev/null Posts: 381
    edited 2009-06-30 19:20
    Here it is:

    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;
    }
  • dev/nulldev/null Posts: 381
    edited 2009-06-30 19:27
    I see the problem, but if I cant return variables my code will get messy... Is there a way I can destroy the local variables?
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2009-06-30 19:44
    You could use the Format class.

    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
  • dev/nulldev/null Posts: 381
    edited 2009-06-30 20:08
    ah ye, thats nice. Thanks Peter!!!
Sign In or Register to comment.