What I tried above didn't help, actually. My Buffer is still messed up after I free the Line pointer. Now my code is like this.
Code:
int PutSthInBuffer (unsigned char *Buffer, int length) {
unsigned char *Path;
unsigned char *Type;
unsigned char *Line;
Line = calloc (SIZE, 1);
//
// read a line, then call strtok and assign one of the tokens to
// Path. The reading line part can be safely assumed to be taken
// care of (don't want to post extra code).
//
Type = strtok (Line, " ");
Path = strtok (NULL, " ");
Buffer = strcpy (Buffer, Path);
FreeMem (Line, NULL, NULL); // this free memory from whatever
// pointers it's passed.
return 0;
}
void FreeMem (unsigned char *ptr1, unsigned char* ptr2, unsigned
char *ptr3) {
free (ptr1);
free (ptr2);
free (ptr3);
}
Sorry, it's quite messy where I'm calling FreeMem, but it should work the same right? It should just free whatever memory was allocated to Line, but it shouldn't affect whatever I copied to Buffer. However, it is still messing up Buffer when I free Line. Any suggestion? Thanks a lot.