Archived

This topic is now archived and is closed to further replies.

winsoft2003

Help:Memory Leak using CreateDIBSection( ) int Win98.

Recommended Posts

I found memory leak when using CreateDIBSection( ).The detail is that after starting my application and closing my application several times,The Bitmap couldn't be successfully created when I restarted my application.The code is like the following.Thanks!(The Application is based on MFC,SDI)
    
//the variable definition

CDC	  m_MemDC; 
HBITMAP	  m_hBitmap;
HBITMAP   m_hOldBitmap;
void	  *m_pBits;

//OnInitialUpdate( )

void CMapView::OnInitialUpdate()
{
    CScrollView::OnInitialUpdate();

    CSize sizeTotal;
    sizeTotal.cx = sizeTotal.cy = 100;
    SetScrollSizes(MM_TEXT, sizeTotal);

//Memery DC

    CDC *pDC = GetDC( );
    m_MemDC.CreateCompatibleDC(pDC);

    BITMAPINFOHEADER bih;
    ZeroMemory( &bih, sizeof(bih) );
    bih.biBitCount = 24;
    bih.biClrImportant = 0;
    bih.biClrUsed = 0;
    bih.biCompression = 0;
    bih.biHeight = 5000;
    bih.biPlanes = 1;
    bih.biSize = sizeof(bih);
    bih.biWidth = 5000;
    bih.biXPelsPerMeter = 0;
    bih.biYPelsPerMeter = 0;

    m_hBitmap = ::CreateDIBSection( pDC->m_hDC, (BITMAPINFO*)&bih, DIB_RGB_COLORS,&m_pBits, NULL, 0 );
    if(m_hBitmap==NULL)
	MessageBox("bitmap failed");
	
    m_hOldBitmap = (HBITMAP)SelectObject( m_MemDC.m_hDC, m_hBitmap );

    ReleaseDC(pDC);
}

//OnDestroy( )

void CMapView::OnDestroy() 
{
    CScrollView::OnDestroy();

    m_MemDC.SelectObject( m_hOldBitmap );
    m_MemDC.DeleteDC( );
    DeleteObject( m_hBitmap );
}
    
[edited by - mittens on May 2, 2003 12:28:11 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
i think the problem might be the "m_MemDC.SelectObject( m_hOldBitmap )" statement. you''re passing in a HBITMAP when you should be passing in a CBitmap* . if you change that to "SelectObject( m_MemDC.m_hDC, m_hOldBitmap )" it might clear up the problem.

Share this post


Link to post
Share on other sites