Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. memory leak when using fstream,seek for help

memory leak when using fstream,seek for help

Scheduled Pinned Locked Moved C / C++ / MFC
csharpiosvisual-studiodebuggingperformance
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    white jungle
    wrote on last edited by
    #1

    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 at if ( (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!

    B 1 Reply Last reply
    0
    • W white jungle

      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 at if ( (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!

      B Offline
      B Offline
      benjymous
      wrote on last edited by
      #2

      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!

      W 1 Reply Last reply
      0
      • B benjymous

        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!

        W Offline
        W Offline
        white jungle
        wrote on last edited by
        #3

        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!

        B 1 Reply Last reply
        0
        • W white jungle

          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!

          B Offline
          B Offline
          Blake Miller
          wrote on last edited by
          #4

          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);

          W 1 Reply Last reply
          0
          • B Blake Miller

            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);

            W Offline
            W Offline
            white jungle
            wrote on last edited by
            #5

            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!

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups