BOOL CGDIPClockDlg::ImageFromIDResource(UINT nID, LPCTSTR sTR,Image * &pImg)
{
HINSTANCE hInst = AfxGetResourceHandle();
HRSRC hRsrc = ::FindResource (hInst,MAKEINTRESOURCE(nID),sTR); // type
if (!hRsrc)
return FALSE; // load resource into memory
DWORD len = SizeofResource(hInst, hRsrc);
BYTE* lpRsrc = (BYTE*)LoadResource(hInst, hRsrc);
if (!lpRsrc)
return FALSE; // Allocate global memory on which to create stream
HGLOBAL m_hMem = GlobalAlloc(GMEM_FIXED, len);
BYTE* pmem = (BYTE*)GlobalLock(m_hMem);
memcpy(pmem,lpRsrc,len);
IStream* pstm;
CreateStreamOnHGlobal(m_hMem,FALSE,&pstm);

// load from stream
pImg=Gdiplus::Image::FromStream(pstm); // free/release stuff
GlobalUnlock(m_hMem);
pstm->Release();
FreeResource(lpRsrc);}GlobalUnlock(m_hMem);之后,m_hMem指向的内存就会自动释放么?
  是否需要  GlobalFree(pSD); ??

解决方案 »

  1.   

    unlock只是解除锁定,释放需要另外的动作
      

  2.   

      不调用
       GlobalFree(m_hMem); 
     是不是就有内存泄露?
      

  3.   

    这要看CreateStreamOnHGlobal是怎么调用的
    WINOLEAPI CreateStreamOnHGlobal(
      __in          HGLOBAL hGlobal,
      __in          BOOL fDeleteOnRelease,
      __out         LPSTREAM* ppstm
    );fDeleteOnRelease 
    A value that indicates whether the underlying handle for this stream object should be automatically freed when the stream object is released. If set to FALSE, the caller must free the hGlobal after the final release. If set to TRUE, the final release will automatically free the hGlobal parameter.如果第二个参数为TRUE,就不需要显示的释放hGlobal
      

  4.   


    难道 MSDN 说的有问题?
      

  5.   

    CreateStreamOnHGlobal(m_hMem,FALSE,&pstm);
    这里第二个参数为 FALSE 啊