I have this code:
void getStringWithTag(char* pFile, long sizeSound, char* tag, char* ret, float* currentPos){
char* tagCopy;
tagCopy = (char*)malloc(256 * sizeof(char));
int totalLen = 0;
int tempLen = 0;
memset(tagCopy, 0, 256);
memset(ret, 0, 128);
pFile += ((int)*currentPos);
while (totalLen+(*currentPos) < sizeSound) {
if (*pFile == '<') {
pFile += 5;
totalLen += 5;
while(*pFile != '>'){
*tagCopy = *pFile;
tagCopy++;
pFile++;
totalLen++;
tempLen++;
}
tagCopy -= tempLen;
tempLen = 0;
if (strcmp(tagCopy, tag) == 0) {
pFile++;
totalLen++;
while (*pFile != '<') {
*ret = *pFile;
ret++;
pFile++;
totalLen++;
tempLen++;
}
ret -= tempLen;
*currentPos += totalLen;
tagCopy = NULL;
free(tagCopy);
return;
}else{
memset(tagCopy, 0, 256);
}
}
pFile++;
totalLen++;
}
tagCopy = NULL;
free(tagCopy);
*currentPos = sizeSound;
ret = NULL;
}
That apparently gives me a memory leak with “tagCopy”.
Can anyone tell me why? I thought I had this down pretty good but this is the only place where I get a memory leak.
Thank you.
You modify
tagCopya few times in that routine, and then you try to free it later. Probably dangerous. You’re also settingtagCopytoNULLbefore callingfree(), so every time you try to freeNULLrather than the actual memory you allocated.