I'm running into a _very_ weird error where I call malloc() three times
for three pointers and get three unique memory pointers as I would
expect, at the end of my loop I free them all and set the pointers to
NULL. Next time around I call malloc three times for the same pointers
and I get back three pointers that are the same. Eg.
first time -
pt1 = 0x12228
pt2 = 0x12630
pt3 = 0x12a38
second time -
pt1 = 0x12228
pt2 = 0x12228
pt3 = 0x12228
and I'll keep getting the same result again and again...
Now, a co-worker has raised a question about if this is a result of me
misusing strtok_r(), I think I'm using it correctly but not sure. Heres
a snippit from the code
printf("Main loop: readBuf->%s<-",readBuf);
// Grab more memory
tempBuf = malloc(MAXMSG);
printf("tempBuf\n");
tokBuf = malloc(MAXMSG);
printf("tokBuf\n");
//tokPtr = malloc(MAXMSG);
//printf("tokPtr\n");
if ( (tempBuf == NULL) || (tokBuf ==
NULL) || (tokPtr == NULL) ){
printf("Memory allocation
error!\n");
exit(-1);
}
printf("Main loop:
tempBuf->%s<-\n",tempBuf);
printf("Main loop: tokBuf->%s<-\n",tokBuf);
printf("Main loop: tempBuf
->%x\n",(unsigned int) tempBuf);
printf("Main loop: tokBuf
->%x\n",(unsigned int) tokBuf);
printf("Main loop: tokPtr
->%x\n",(unsigned int) tokPtr);
strncpy(tempBuf, readBuf, MAXMSG);
memset(tokBuf, 0, MAXMSG);
memset(tokPtr, 0, MAXMSG);
printf("Main loop:
tempBuf->%s<-\n",tempBuf);
tokBuf = strtok_r(tempBuf,":",tokPtr);
printf("Main loop: tokBuf->%s<-\n",tokBuf);
if ( tokBuf == NULL ) {
printf("Main loop: Invalid
command\n");
}
<-- SNIP -->
free(tempBuf);
//free(tokPtr);
free(tokBuf);
tempBuf = NULL;
//tokPtr = NULL;
tokBuf = NULL;
This gets looped everytime the program recives a message from a socket
that is connected earlier.
Any ideas?
Jason
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/ts-7000/
<*> Your email settings:
Individual Email | Traditional
<*> To change settings online go to:
http://groups.yahoo.com/group/ts-7000/join
(Yahoo! ID required)
<*> To change settings via email:
<*> To unsubscribe from this group, send an email to:
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
|