Question

I have this extremely strange behavior coming :

In the below code: If I comment the call to MyLogger then everything works fine that is I get the sTempNr tokenized and 4 tokens are printed . But if I uncomment the call to MyLogger for logging then only the iteration takes place once and in other testing class with similar code as below there is a looping taking place more than 4 times with redundant info being sent to MyLogger.

So, I checked with Purify tool to determine if there were some memory issues in MyLogger. Could not find any. MyLogger is using vaargs to extract args and vfprintf call to print.

I am not sure how to debug this code further. Any guidance would be appreciated!.

char sTempNr[41] = "1129Z13589.2.9.10";
char *sTempStr;
sTempStr = NULL;

sTempStr = strtok(sTempNr,".");
while (sTempStr)
{
     printf("in in TempStr[%s]\n",sTempStr);
      //MyLogger("write","","Temp String[%s]",sTempStr);

     sTempStr = strtok(NULL,".");
}
Was it helpful?

Solution

strtok() keeps some static data inside so probably MyLogger calls strtok(), either directly or indirectly.

Replace strtok() with strtok_r() (reentrant version of strtok()) to get around this problem.

OTHER TIPS

Is the logger calling strtok?

Is MyLogger using strtok as well? Note that strtok is not stateless.

Sounds like MyLogger is corrupting the string in sTempStr. Try printing it out again after the call to MyLogger. eg

 printf("in in TempStr[%s]\n",sTempStr);
 MyLogger("write","","Temp String[%s]",sTempStr);
 printf("in in TempStr[%s]\n",sTempStr);

and see if anything has changed

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top