[英]Valgrind reports a memory leak when defining a char* array
I have another valgrind error that I can't seem to figure out. 我还有另一个无法识别的valgrind错误。 I have some code that loads a script then uses strtok() to tokenize the data in said script that has been loaded into a buffer.
我有一些代码加载脚本,然后使用strtok()标记已加载到缓冲区的脚本中的数据。 I allocate memory on the heap for my buffer using the following:
我使用以下方法在堆上为缓冲区分配内存:
char *pPngStr = new char[4096];
Then at the end of the function, I free the memory using: 然后在函数结尾,我使用以下命令释放内存:
delete[] pPngStr;
Valgrind reports the allocation as so: Valgrind如此报告分配:
173 bytes in 1 blocks are definitely lost in loss record 3,753 of 4,627
Valgrind also reports the line where I free the memory as: Valgrind还报告了我释放内存的行:
Invalid free() / delete / delete[] / realloc()
This is all in the same function is the variable is local to only that function and not the class. 这全部在同一个函数中,变量仅在该函数而不是类中是局部的。 I am at a loss as to how valgrind thinks that this is a memory leak.
我不知道valgrind如何认为这是内存泄漏。 I am allocating all the memory I need then I am freeing it when I am done.
我分配了我需要的所有内存,然后在完成后释放它。 I don't see what the problem is.
我看不出问题是什么。
Here is the complete function (it may be a little messy): 这是完整的功能(可能有点混乱):
void CSprite::LoadSprite()
{
FILE *pF = fopen("Data/Art/coin/coin.sprite", "rb");
if(!pF)
{
return;
}
fseek(pF, 0, SEEK_END);
int length = ftell(pF);
fseek(pF, 0, SEEK_SET);
char *aData = new char[length];
fread(aData, 1, length, pF);
CheckGLError();
/**
* The scripts are setup like so:
***********************************************
* Pngs:
* 4; // This is the number of frames.
* <pngname>_001.png;
* <pngname>_002.png;
* <pngname>_003.png;
* <pngname>_004.png;
*
* Properties:
* name=<obj name>;
* width=###;
* height=###;
* framerate=0.### // rate in seconds
************************************************
*/
mpSprite = new SGfxSprite;
char *pPngStr = new char[4096];
pPngStr = strtok(aData, "\n");
pPngStr = strtok(NULL, "\n");
int iNumFrames = atoi(pPngStr);
mpSprite->numFrames = iNumFrames;
mpSprite->textures = new GLuint[iNumFrames];
glGenTextures(iNumFrames, mpSprite->textures);
int i = 0; // used for loop only!
while(pPngStr != NULL)
{
pPngStr = strtok(NULL, "\n");
if(!strcmp(pPngStr, "Properties:") || i >= iNumFrames)
{
break;
}
if(mpPng->LoadPng(pPngStr))
{
CheckGLError();
glBindTexture(GL_TEXTURE_2D, mpSprite->textures[i]);
CheckGLError();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
CheckGLError();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
CheckGLError();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
CheckGLError();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
CheckGLError();
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
CheckGLError();
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, mpPng->miWidth, mpPng->miHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, mpPng->mpData);
CheckGLError();
}
++i;
}
pPngStr = strtok(NULL, "=\n");
pPngStr = strtok(NULL, "\n");
mpSprite->name = pPngStr;
pPngStr = strtok(NULL, "=\n");
pPngStr = strtok(NULL, "\n");
mpSprite->width = atoi(pPngStr);
pPngStr = strtok(NULL, "=\n");
pPngStr = strtok(NULL, "\n");
mpSprite->height = atoi(pPngStr);
pPngStr = strtok(NULL, "=\n");
pPngStr = strtok(NULL, "\n");
mpSprite->framerate = (atof(pPngStr) * 1000);
mpSprite->x = 0;
mpSprite->y = 0;
mpSprite->currentFrame = 0;
mpSprite->paintTimer = 0;
fclose(pF);
delete[] pPngStr, aData;
} }
*PLEASE NOTE: this function contains OpenGL code and also invokes members from data structures that are not defined here. *请注意:此函数包含OpenGL代码,并且还会从此处未定义的数据结构中调用成员。 The code also compiles without any warnings or errors in Xubuntu 12.04 with g++ (gcc 4.6) using Qt Creator 2.4.1
使用Qt Creator 2.4.1在带有g ++(gcc 4.6)的Xubuntu 12.04中编译代码时也不会出现任何警告或错误。
char *pPngStr = new char[4096];
pPngStr = strtok(aData, "\n");
Just in the line after the new, you are forgetting about the pointer that you go and using the variable to store other data. 在新代码之后的一行中,您忘记了要使用的指针,而是使用该变量存储其他数据。 No doubt that there is a memory leak.
毫无疑问,内存泄漏。
The line that you have: 您拥有的行:
delete[] pPngStr, aData;
does not do what you think it does. 不按照您的想法去做。
Try this instead: 尝试以下方法:
delete[] pPngStr;
delete[] aData;
Note : Until you fix the bug that SJuan76 identifies, my answer is moot. 注意 :在修复SJuan76识别的错误之前,我的回答还没有定论。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.