I am trying to use CGridCtrl inside of CCtrlView, in an MDI app. And I wrote this:
and in cpp:Code:class CTestGridView : public CCtrlView { protected: // create from serialization only CTestGridView(); DECLARE_DYNCREATE(CTestGridView) // Attributes public: CTestGridDoc* GetDocument() const; CGridCtrl& GetGridCtrl() const {return *(CGridCtrl*)this;} .... }
Until here, it goes without problem ... but when I am trying to use CGridCtrl object inside, I get a crash:Code:CTestGridView::CTestGridView() :CCtrlView(GRIDCTRL_CLASSNAME, AFX_WS_DEFAULT_VIEW) { // TODO: add construction code here WNDCLASS wndcls; //HINSTANCE hInst = AfxGetInstanceHandle(); HINSTANCE hInst = AfxGetResourceHandle(); if(! (::GetClassInfo(hInst, GRIDCTRL_CLASSNAME, &wndcls))) { // otherwise we need to register a new class wndcls.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW; wndcls.lpfnWndProc = ::DefWindowProc; wndcls.cbClsExtra = wndcls.cbWndExtra = 0; wndcls.hInstance = hInst; wndcls.hIcon = NULL; #ifndef _WIN32_WCE_NO_CURSOR wndcls.hCursor = AfxGetApp()->LoadStandardCursor(IDC_ARROW); #else wndcls.hCursor = 0; #endif wndcls.hbrBackground = (HBRUSH) (COLOR_3DFACE + 1); wndcls.lpszMenuName = NULL; wndcls.lpszClassName = GRIDCTRL_CLASSNAME; if(! AfxRegisterClass(&wndcls)) { AfxThrowResourceException(); } } }
and the error is:Code:void CTestGridView::OnInitialUpdate() { CCtrlView::OnInitialUpdate(); CGridCtrl& GridCtrl = (CGridCtrl&)GetGridCtrl(); GridCtrl.SetRowCount(1); // <-- crash }
Code:Unhandled exception at 0x77ce0ebc (mfc90d.dll) in TestGrid.exe: 0xC0000005: Access violation reading location 0xfeeefeee.This error has been triggered when grid is trying to call:Code:if (!AfxIsValidAddress(pOb, pOb->GetRuntimeClass()->m_nObjectSize, FALSE)) { TRACE(traceAppMsg, 0, "ASSERT_VALID fails with illegal pointer.\n"); if (AfxAssertFailedLine(lpszFileName, nLine)) AfxDebugBreak(); return; // quick escape }
Is obvious that I done something wrong ... but what ? It is possible what I am trying to do ?Code:TRY { m_arRowHeights.SetSize(nRows); // <-- here is firing the crash
Thank you for any hint/help in solving my task.
P.S. I attach an test app that illustrate the problem.




Reply With Quote
