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