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. Need help for memory leak

Need help for memory leak

Scheduled Pinned Locked Moved C / C++ / MFC
graphicsperformancehelpquestionannouncement
3 Posts 2 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.
  • V Offline
    V Offline
    Vincent Ye
    wrote on last edited by
    #1

    Hi All, I use the code below to load a bitmap file to my program. void CStartupDlg::LoadPictureFile(HDC hdc, LPCTSTR szFile, CBitmap *pBitmap, CSize &mSize) { // open file HANDLE hFile = CreateFile(szFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL); _ASSERTE(INVALID_HANDLE_VALUE != hFile); // get file size DWORD dwFileSize = GetFileSize(hFile, NULL); _ASSERTE(-1 != dwFileSize); LPVOID pvData = NULL; // alloc memory based on file size HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwFileSize); _ASSERTE(NULL != hGlobal); pvData = GlobalLock(hGlobal); _ASSERTE(NULL != pvData); DWORD dwBytesRead = 0; // read file and store in global memory BOOL bRead = ReadFile(hFile, pvData, dwFileSize, &dwBytesRead, NULL); _ASSERTE(FALSE != bRead); GlobalUnlock(hGlobal); CloseHandle(hFile); // create IStream* from global memory LPSTREAM pstm; HRESULT hr = CreateStreamOnHGlobal(hGlobal, TRUE, &pstm); _ASSERTE(SUCCEEDED(hr) && pstm); // Create IPicture from image file hr = ::OleLoadPicture(pstm, dwFileSize, FALSE, IID_IPicture, (LPVOID *)&gpPicture); <==== It said the memory allocated here is not released. _ASSERTE(SUCCEEDED(hr) && gpPicture); pstm->Release(); ........................................................................... gpPicture->Release(); } After I use "Rational Purify" to check memory leak, it reports a memory leak happened. Could you tell me what I shd release? Thanks in advance Vincent

    D 1 Reply Last reply
    0
    • V Vincent Ye

      Hi All, I use the code below to load a bitmap file to my program. void CStartupDlg::LoadPictureFile(HDC hdc, LPCTSTR szFile, CBitmap *pBitmap, CSize &mSize) { // open file HANDLE hFile = CreateFile(szFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL); _ASSERTE(INVALID_HANDLE_VALUE != hFile); // get file size DWORD dwFileSize = GetFileSize(hFile, NULL); _ASSERTE(-1 != dwFileSize); LPVOID pvData = NULL; // alloc memory based on file size HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwFileSize); _ASSERTE(NULL != hGlobal); pvData = GlobalLock(hGlobal); _ASSERTE(NULL != pvData); DWORD dwBytesRead = 0; // read file and store in global memory BOOL bRead = ReadFile(hFile, pvData, dwFileSize, &dwBytesRead, NULL); _ASSERTE(FALSE != bRead); GlobalUnlock(hGlobal); CloseHandle(hFile); // create IStream* from global memory LPSTREAM pstm; HRESULT hr = CreateStreamOnHGlobal(hGlobal, TRUE, &pstm); _ASSERTE(SUCCEEDED(hr) && pstm); // Create IPicture from image file hr = ::OleLoadPicture(pstm, dwFileSize, FALSE, IID_IPicture, (LPVOID *)&gpPicture); <==== It said the memory allocated here is not released. _ASSERTE(SUCCEEDED(hr) && gpPicture); pstm->Release(); ........................................................................... gpPicture->Release(); } After I use "Rational Purify" to check memory leak, it reports a memory leak happened. Could you tell me what I shd release? Thanks in advance Vincent

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      vincentye wrote: HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwFileSize); Where is the matching call to GlobalFree()? See here for an example that complements the Loadpic.exe example from MSDN. Here is a class example.


      "When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen

      V 1 Reply Last reply
      0
      • D David Crow

        vincentye wrote: HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwFileSize); Where is the matching call to GlobalFree()? See here for an example that complements the Loadpic.exe example from MSDN. Here is a class example.


        "When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen

        V Offline
        V Offline
        Vincent Ye
        wrote on last edited by
        #3

        Thx for your reply. I did try GlobalFree(hGlobal), follow the code of "pstm->Release()", but Purity told me I am trying to release an unallocated memory. Where shd I put the GlobalFree() function in this case? If I put it before pstm->Release(), Purify told me this message : "[W] PAR: Global/LocalLock(0x571001c) arg #1 (hMem) attempt to lock handle that is on pending free queue. {1 occurrence}" Thx again Vincent

        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