Help With Memory Leak


i writing fifo buffer output 12x lines of status tft display.

the array of pointers using sent in, , new data pushed onto array.  using strdup() in order move data around , running out of sram on mega quite fast; after 5 or 6 pushes.

can me alternative strdup or better way free() memory?

code: [select]


#define screen_buffer_sz 12
char *tftbuffer[screen_buffer_sz]={0};
...
in setup:

initbuffer(tftbuffer);

somewhere in loop:

pushbuffer(tftbuffer,"serial command received...");

void initbuffer (char** data) {

for (int i=0; < screen_buffer_sz; i++) {
data[i]=" ";
}
}

void pushbuffer (char** data, char* str) {
char* localbuffer[1];
char* localbuffer2[1];
int x,y;

// clear out previous characters
for (int i; < screen_buffer_sz; i++) {
x = 150 - (10*i);
y=20;
tft.drawstring((char*) data[i],x,y,1,black);
}

for (int i=screen_buffer_sz-1; i>=0; i--) {
if (i==11) {
// save existing data[11]
localbuffer[0]=strdup(data[i]);
// insert new string @ data[11]
//localbuffer2[0]=strdup(str);
data[i]=strdup(str);
    } else {
// copy existing data n
localbuffer2[0]=strdup(data[i]);
// insert n-1 data @ n
data[i]=strdup(localbuffer[0]);
    localbuffer[0]=strdup(localbuffer2[0]);
}
}
//free(str);
free(localbuffer[0]);
free(localbuffer2[0]);
}

here terminal data, doing sending in serial commands code:

boomer online @ 10.0.0.177
serial command received
free ram: 5777
serial command received
free ram: 5570
serial command received
serial command received
serial command received
free ram: 4499
serial command received
free ram: 3992
serial command received
free ram: 3410


Arduino Forum > Using Arduino > Programming Questions > Help With Memory Leak


arduino

Comments

Popular posts from this blog

invalid use of void expresion in FlexiTimer2 library

error: a function-definition is not allowed here before '{' token

LED Strip Code