开发者

ipad, Need help with a memory leak

开发者 https://www.devze.com 2023-01-07 04:33 出处:网络
I have this code: void getStringWithTag(char* pFile, long sizeSound, char* tag, char* ret, float* currentPos){

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 tagCopy a few times in that routine, and then you try to free it later. Probably dangerous. You're also setting tagCopy to NULL before calling free(), so every time you try to free NULL rather than the actual memory you allocated.

0

精彩评论

暂无评论...
验证码 换一张
取 消