From 952d1398f5fa9a8530361afeb6e79314b1707f45 Mon Sep 17 00:00:00 2001 From: Christian Kroll Date: Sun, 2 Aug 2009 13:59:41 +0000 Subject: [PATCH] fixed a memory leak and clarified the comment on the strtok_r problem --- scrolltext/scrolltext3.c | 32 +++++++++++--------------------- 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/scrolltext/scrolltext3.c b/scrolltext/scrolltext3.c index 956594c..1b9419d 100644 --- a/scrolltext/scrolltext3.c +++ b/scrolltext/scrolltext3.c @@ -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 }