fixed a memory leak and clarified the comment on the strtok_r problem

This commit is contained in:
Christian Kroll 2009-08-02 13:59:41 +00:00
parent 37e814ce51
commit 952d1398f5
1 changed files with 11 additions and 21 deletions

View File

@ -103,6 +103,9 @@ typedef struct blob_t_struct{
const unsigned char* fontData;
unsigned char font_storebytes;/*bytes per char*/
unsigned char space;
#ifndef AVR
char scrolltextBuffer[SCROLLTEXT_BUFFER_SIZE];
#endif
}blob_t;
@ -280,29 +283,23 @@ unsigned char blobNextCommand(blob_t * blob){
blob_t * setupBlob(char * str){
#ifndef AVR
// strtok_r must not be used on string literals so we copy the string to
// the heap (at least on non-AVR based processors)
int n;
char *scrolltext = NULL;
if ((str != NULL) && ((n = (strlen(str))) != 0)) {
scrolltext = malloc(n + 1);
strcpy(scrolltext, str);
str = scrolltext;
}
#endif
static unsigned char chop_cnt;
static char *last; static char delim[] = "#";
static char *lastcommands;
unsigned int tmp;
blob_t *blob = malloc(sizeof (blob_t));
if(str){
#ifndef AVR
// on non-AVR archs strtok_r fails for some reason if it operates on a
// string which is located on another stack frame, so we need our own copy
memcpy (&blob->scrolltextBuffer, str, SCROLLTEXT_BUFFER_SIZE);
str = &blob->scrolltextBuffer;
#endif
chop_cnt = 0;
}
blob_t *blob = malloc(sizeof (blob_t));
if(!chop_cnt){
blob->commands = strtok_r (str, delim, &last);
@ -369,13 +366,6 @@ blob_t * setupBlob(char * str){
fail:
free(blob);
#ifndef AVR
if (scrolltext != NULL) {
free(scrolltext);
}
#endif
return 0;//no more blobs to parse
}