CPaintDC - Unhandled Exception

Hello,
I have started writing a Grid control derived from CWnd. I am using VC++ 6 with SP5
The architecture is based on Chris Maunder's MFC Grid Control from CodeProject.com. I have used CTypedPtrArray to allocate memory for Grid cells. I have also used a Memory device context in Paint to stop flicker. The code works fine till I load 500 items in the grid. Beyond that I get a debug assertion in my Paint procedure in this line,
CBitmap *oldBitmap = memDC.SelectObject(&bm); I checked during debug and found out that memDC becomes NULL. I tried not using memDC, but the problem still continues..this time with the dc of CPaintDC itself. The Release version just blanks out the entire screen. I am not able to understand, please help.

Thanks,
Balaji

Here are the relevant portions in the code:

BOOL CGridCtrl::OnEraseBkgnd(CDC* pDC)
{
      return (TRUE);
}

void CGridCtrl::OnPaint()
{
      CPaintDC dc(this);

      CRect rect;
      GetClientRect(&rect);

      //CMemDC memDC(&dc);
      //OnDraw(&memDC);

      CDC memDC;
      memDC.CreateCompatibleDC(&dc);

      CBitmap bm;
      bm.CreateCompatibleBitmap(&dc,rect.Width(),rect.Height());
      CBitmap *oldBitmap = memDC.SelectObject(&bm);

      //Erase the background here

      CBrush brush;
      brush.CreateSolidBrush(GetSysColor(COLOR_WINDOW));
      memDC.FillRect(&rect,&brush);
      brush.DeleteObject();

                //Draw the Grid
      OnDraw(&memDC);
      
      dc.BitBlt(rect.left,rect.top,rect.Width(),rect.Height(),&memDC,0,0,SRCCOPY);

      memDC.SelectObject(oldBitmap);
      bm.DeleteObject();
      memDC.DeleteDC();
}
BalajiSAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Roshan DavisCommented:
Create a bitmap compatible with the screen that contains one more scanline than the original bitmap may make problems...

   hBMPCompatible = CreateCompatibleBitmap(hDCScreen, 801, 601);   <-- This makes problems,

Becoz u are creating that bitmap compatible with current resolution screen.

So if size exceeds the screen resolution, makes it to the resolution.

Rosh :)
0
Roshan DavisCommented:
This is the general case, but u are taking the rect from ClientRect, this should not make problems....

checking.......
0
BalajiSAuthor Commented:
One more point to be noted: I added these lines now and observed the following,

CDC memDC;
memDC.CreateCompatibleDC(&dc); //memDC becomes null !....Unable to create memDC ?
HDC hDC = memDC.GetSafeHdc(); //Therefore hDC too is null !

This is not the case if I load the Grid with fewer items. One more thing is that the Grid's rect is unaltered, only scrollbars are added.

Thanks,

Balaji
0
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

Roshan DavisCommented:
Just try this

Create memory dc once like

Create a pointer member of memory dc

if ( !m_pMemDC )
{
      m_pMemDC = new CDC;

      m_pMemDC->CreateCompatibleDC(&dc);

    bm.CreateCompatibleBitmap(&dc,nScreenWidth,nScreenHieght);
    m_oldBitmap = m_pMemDC->SelectObject(&bm);

}

CBrush brush;
brush.CreateSolidBrush(GetSysColor(COLOR_WINDOW));
m_pMemDC->FillRect(&rect,&brush);
brush.DeleteObject();

//Draw the Grid
OnDraw(m_pMemDC);

Rosh :)
0
AndyAinscowFreelance programmer / ConsultantCommented:
Rosh

if ( !m_pMemDC )
{
     m_pMemDC = new CDC;

     m_pMemDC->CreateCompatibleDC(&dc);

    bm.CreateCompatibleBitmap(&dc,nScreenWidth,nScreenHieght);
    m_oldBitmap = m_pMemDC->SelectObject(&bm);

}

also requires a bitmap that exists even when the onPaint routine is finished

It should read

if ( !m_pMemDC )
{
     m_pMemDC = new CDC;

     m_pMemDC->CreateCompatibleDC(&dc);
}
bm.CreateCompatibleBitmap(&dc,nScreenWidth,nScreenHieght);
m_oldBitmap = m_pMemDC->SelectObject(&bm);

 and just to be real awkward what happens if the user changes the resolution/colour depth whilst the app is running.

0
Roshan DavisCommented:
CreateCompatibleBitmap will not copy bitmaps from one DC to another, so it need only once.
Changing resolution makes problems in the above code, that need to handled...
Rosh :)
0
BalajiSAuthor Commented:
Thanks for the new idea...I just tried it now. Still the problem continues...during debug, CreateCompatibleDC fails once again...

if (!m_pMemDC)
{
      m_pMemDC = new CDC;
      m_pMemDC->CreateCompatibleDC(&dc);
      HDC hDC = m_pMemDC->GetSafeHdc();  //hDC is NULL here....!
}

Balaji
0
Roshan DavisCommented:
m_pMemDC should be the class member, so it will be created once....So put the declaration of that memory pointer in the class header

Rosh :)
0
BalajiSAuthor Commented:
Yes, I did that, but still the problem continues..... In fact, just to check, I tried this too..
HDC hdcMem = ::CreateCompatibleDC(dc.GetSafeDC()); //hdcMem is NULL ?

Balaji
0
Roshan DavisCommented:
When u are deleting that memory DC? That should be deleted in the destructor

Please mention the ClientRect value of that ASSERT iteration
Rosh :)
0
AndyAinscowFreelance programmer / ConsultantCommented:
Yes, but if bm is a local variable it goes out of scope when OnPaint exits and at the next call you have something like

CBitmap bm;
CBitmap *oldBitmap = memDC.SelectObject(&bm);
paint to memDC...
0
Roshan DavisCommented:
Thats true, Use API-::CreateCompatibleBitmap that gives HBITMAP
0
AndyAinscowFreelance programmer / ConsultantCommented:
Post exam stress?
0
Roshan DavisCommented:
?? I didn't get
Rosh :)
0
AndyAinscowFreelance programmer / ConsultantCommented:
It's usually pre- not post-   (dim memories from the past).

ps.  Anything less than a first and I would be very surprised based on your knowledge at EE.
0
Roshan DavisCommented:
>>>> Post exam stress?
>> It's usually pre- not post-   (dim memories from the past).
Still I didn't get :o(
0
AndyAinscowFreelance programmer / ConsultantCommented:
Why :o(
I remember looking at massive folders full of paper and thinking I should know all of this.  (But I worry about trivial things).
0
Roshan DavisCommented:
:0)
0
BalajiSAuthor Commented:

Yes, I am deleting m_pMemDC in my destructor only.

I have copied the code where it asserts while in the Debug mode:

if (!m_pMemDC)
{
     m_pMemDC = new CDC;
     m_pMemDC->CreateCompatibleDC(&dc); //Didn't get created here....
     -----------------------------------------------
}
bm->CreateCompatibleBitmap(&dc,rect.Width(),rect.Height());
CBitmap *oldBitmap = m_pMemDC->SelectObject(bm); //Asserts here becasue m_pMemDC's m_hDC is null ! Its just refusing to create a memory device context for me !

Also checked the ClientRect: It is a valid one...RECT(0,289,0,445)

MFC Source Code:

_AFXWIN_INLINE CBitmap* CDC::SelectObject(CBitmap* pBitmap)
{ ASSERT(m_hDC != NULL); return (CBitmap*) SelectGdiObject(m_hDC,
pBitmap-GetSafeHandle()); }

Strangely, the same code works well with fewer items in the Grid....just to remind you of that !
This is the first time I have encountered such a problem....very very strange but ITS TRUE !

Balaji



0
_nn_Commented:
If the code works with fewer items in the grid, and suddenly fails when that number grows, then it must have something to do with GDI objects being allocated for each grid item. At some point, GDI runs out of resources and can't allocate a new DC. What is GetLastError() when the memory DC creation fails ?
0
BalajiSAuthor Commented:
That seems to be a valid reason why a dc cannot be created. BTW, GetLastError() returns 0, doesn't seem to convey anything, does it ?

Balaji
0
AndyAinscowFreelance programmer / ConsultantCommented:
Just for interest what happens if you don't try drawing offscreen.
Comment out your code for the memdc etc. and draw it directly on the given dc.
Does that function (apart from flicker) correctly?
What happens if you add even more rows (re gdi object theory).
0
BalajiSAuthor Commented:
After commenting out the MemDC part, the problem still continues, only this time it fails to create a CPen GDI object. Memory allocation with CTypedPtrArray seems to affect GDI.
Please help me in this regard.

Balaji

Relevant portions in code:

typedef CTypedPtrArray<CObArray,CGridCellBase*> GRID_ROW;
//..............................................................................................
//In the Class header section:
CTypedPtrArray<CObArray,GRID_ROW*> m_RowData;

//Initialize function:

TRY
{
m_RowData.SetSize(m_nRows); //m_nRows = 1200 !!!

//Here's the problem...Too much.....? ASSERTS in OnPaint(), works fine till 500 items.
Strangely does not get into my CATCH statement here.

for (int nRow = 0; nRow < m_nRows; nRow++)
{
m_RowData[nRow] = new GRID_ROW;
m_RowData[nRow]->SetSize(m_nCols);

for (int nCol = 0; nCol < m_nCols; nCol++)
{
GRID_ROW *pRow = m_RowData[nRow];
pRow->SetAt(nCol,CreateCell(nRow,nCol));
}
}
}
CATCH (CMemoryException,e)
 {
e->ReportError();
return FALSE;
 }
END_CATCH

0
AndyAinscowFreelance programmer / ConsultantCommented:
What is in your CreateCell?

Are you creating a CPen for each cell?  (Bad idea, use one, or a few,  commen CPen objects)
0
BalajiSAuthor Commented:
This is what is there in CreateCell. After this, in the OnPaint procedure I enumerate all cells for all rows and each cell draws itself given the CDC * from Paint.

CGridCellBase* CGridCtrl::CreateCell(int nRow,int nCol)
{
      if (!m_pRtcDefault || !m_pRtcDefault->IsDerivedFrom(RUNTIME_CLASS(CGridCellBase)))
      {
            ASSERT( FALSE);
            return NULL;
      }
      CGridCellBase* pCell = (CGridCellBase*) m_pRtcDefault->CreateObject();
      if (!pCell)
            return NULL;

      pCell->SetGrid(this);

      return pCell;
}
0
_nn_Commented:
Since CGridCellBase cannot be instantiated, I assume that CreateObject() returns an instance of some descendant. What is m_pRtcDefault and what does it return ?
0
BalajiSAuthor Commented:
GOT IT !!!

Your previous suggestion,

"What is in your CreateCell? Are you creating a CPen for each cell?  (Bad idea, use one, or a few,  commen CPen objects)"

was very valuable indeed. Suddenly it occured to me that I was creating a CFont object for each and every cell in the Grid and selecting it into Device context. I did this in the Cell's constructor. This was causing a GDI Assertion for more than 500 cells, and therefore OnPaint was unable to provide a Non Null dc.

I have now rewritten my code to create the font once from the Grid and passing it on to the cell each time it requires. That has solved my problem.

Thanks a lot,

Balaji

0
AndyAinscowFreelance programmer / ConsultantCommented:
So are you accepting my comment as an answer then?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BalajiSAuthor Commented:
Thanks for that tip ! It helped me search for the problem in a different perspective.

Balaji
0
AndyAinscowFreelance programmer / ConsultantCommented:
Your welcome.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.