memory leak when using fstream,seek for help
-
int main(int argc, char* argv[]) { long start=0,length=0; char *pByte=(char*)malloc(sizeof(char)*10); if(pByte==NULL) return -1; memset(pByte,1,10); ofstream fout("test.bmp",ios::out); if(!fout) { return -1; } fout.write(pByte,10); free(pByte); fout.close(); return 0; }
I debug this program with boundscheker,the bc report that there are 160 bytes memory leak which atif ( (p = (_PVFV *) _realloc_crt(__onexitbegin, _msize_crt(__onexitbegin) + ONEXITTBLINCR * sizeof(_PVFV))) == NULL )//《====Here
these code was copy from "..\Microsoft Visual Studio\VC98\CRT\SRC\onexit.c" I do not know where I am wrong at. please anybody help me! Don't look at me in that way! -
int main(int argc, char* argv[]) { long start=0,length=0; char *pByte=(char*)malloc(sizeof(char)*10); if(pByte==NULL) return -1; memset(pByte,1,10); ofstream fout("test.bmp",ios::out); if(!fout) { return -1; } fout.write(pByte,10); free(pByte); fout.close(); return 0; }
I debug this program with boundscheker,the bc report that there are 160 bytes memory leak which atif ( (p = (_PVFV *) _realloc_crt(__onexitbegin, _msize_crt(__onexitbegin) + ONEXITTBLINCR * sizeof(_PVFV))) == NULL )//《====Here
these code was copy from "..\Microsoft Visual Studio\VC98\CRT\SRC\onexit.c" I do not know where I am wrong at. please anybody help me! Don't look at me in that way!Well, once possible leak is after
if(!fout)
- At that point in your program you exit, without freeing pByte -- Help me! I'm turning into a grapefruit! Buzzwords! -
Well, once possible leak is after
if(!fout)
- At that point in your program you exit, without freeing pByte -- Help me! I'm turning into a grapefruit! Buzzwords!the program goes well. and the fout always has correct value! if I use fopen,fclose. the BoundsChecker report nothing example:
char buff[]="11111"; FILE* pFile=fopen("www.txt","w+"); if(!pFile) return; fwrite(buff,1,5,pFile); fclose(pFile);
Don't look at me in that way! -
the program goes well. and the fout always has correct value! if I use fopen,fclose. the BoundsChecker report nothing example:
char buff[]="11111"; FILE* pFile=fopen("www.txt","w+"); if(!pFile) return; fwrite(buff,1,5,pFile); fclose(pFile);
Don't look at me in that way!Maybe closing the file handle first will flush the buffer and you will be okay :doh: fout.write(pByte,10); fout.close(); // <- call this before freeing memory free(pByte);
-
Maybe closing the file handle first will flush the buffer and you will be okay :doh: fout.write(pByte,10); fout.close(); // <- call this before freeing memory free(pByte);
the meory leak as before!
char *buff=new char[7]; strcpy(buff,"123456"); ofstream fout("test.txt",ios::out); fout.write(buff,6); fout.close(); delete[] buff;
I hope it's boundschecker wrong!:-D Don't look at me in that way!