Link to home
Start Free TrialLog in
Avatar of kawa
kawa

asked on

OnCreate() crashes in debug mode only?

I need to add some custom methods for MS DHTML edit ActiveX control.
I created MFC ActiveX project and inserted MS DHTML control inside it.
I implemented OnCreate as follows:

int CTestCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
 if (COleControl::OnCreate(lpCreateStruct) == -1)
  return -1;

 if (!m_pMyEdit)
  m_pMyEdit = new CDHTMLEdit;

 if (m_pMyEdit)


  CRect rect(0, 0, lpCreateStruct->cx, lpCreateStruct->cy);
  BOOL IsCtrlCreated = m_pMyEdit->Create("Test", "TestWnd", WS_VISIBLE |
WS_CHILD, rect, this, 101);
  if(!IsCtrlCreated )
   AfxMessageBox("Error: Failed to create the control!!") ;
  return 0;
 }
 else
 {
  delete m_pMyEdit;
  return -1;
 }
return 0;
}

and I added to my InitInstance()

 BOOL bInit = COleControlModule::InitInstance();
 if (bInit)
 {
  AfxEnableControlContainer();
}
 return bInit;
}

I also added to stdafx.h the following:
#include <Afxdisp.h>  



The debug OCX crashes in OnCreate() when I try to embed it in HTML.


What am I missing here?
Do I need to implement OnDraw() and How?
And how to expose the focus() and Refresh() methods?

Any help or direction will be appreciated.

Thanks





Avatar of mikeblas
mikeblas

Is the crash in the OLE Control?  Or in your OnCreate() handler in your app?  It's unclear from your question.

Your question really lacks lots of important information:

1) What version of IE are you using?  _Exactly_; from the about box.

2) What version of MFC are you using?

3) What version of which operating system are you using?

4) Please provide a symbolic call stack leading to the crash.

Thank you.

..B ekiM
Avatar of kawa

ASKER

Thanks Mike for taking the time to answer.

The crash is in my OnCreate() handler.

1. IE version is 5.00.2919.6307IC
2. MSVC version is 6.0 enterprise edition (I guess MFC version will be 4.2)
3. OS is Windows NT Server 4.0 + SP5 +Option Pack
4. The crash Message is:

<  Unhandled exception in IEXPLORE.EXE (TESTCTRL.OCX): 0x0000005: Access Violation  >

5. On the stack window I have:

CTestCtrl::OnCreate(tagCREATESTRUCTA * 0x0006d840) line 220 + 47 bytes
CWnd::OnWndMsg(unsigned int 1, unsigned int 0, long 448576, long * 0x0006d6c0) line 1811 + 13 bytes
CWnd::WindowProc(unsigned int 1, unsigned int 0, long 448576) line 1585 + 30 bytes
COleControl::WindowProc(unsigned int 1, unsigned int 0, long 448576) line 1720 + 20 bytes
AfxCallWndProc(CWnd * 0x027e1860 {CMyHtmlEditCtrl hWnd=0x00020152}, HWND__ * 0x00020152, unsigned int 1, unsigned int 0, long 448576) line 215 + 26 bytes
AfxWndProc(HWND__ * 0x00020152, unsigned int 1, unsigned int 0, long 448576) line 368
AfxWndProcDllStatic(HWND__ * 0x00020152, unsigned int 1, unsigned int 0, long 448576) line 57 + 21 bytes
USER32! 77e71303()
USER32! 77e7700b()
NTDLL! 77f763ef()
USER32! 77e77768()

On the Disassembly Window I have

027A10C2   mov         eax,dword ptr [edx]
027A10C4   call        dword ptr [eax+5Ch]
027A10C7   cmp         esi,esp
027A10C9   call        _chkesp (027a2f66)
027A10CE   mov         dword ptr [IsCtrlCreated],eax
221:          if(!IsCtrlCreated )
027A10D1   cmp         dword ptr [IsCtrlCreated],0
027A10D5   jne         CMyHtmlEditCtrl::OnCreate+145h (027a10e5)
222:              AfxMessageBox("Error: Failed to create the control!!") ;


Can these information help you to tell me what is going on?

Thanks again for your time.

Hi,

Modify Ur OnCreate function like this. It wont crash
int CCrashXCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
      if (COleControl::OnCreate(lpCreateStruct) == -1)
            return -1;

      if (!m_pMyEdit)
            m_pMyEdit = new CDHTMLEdit;

      if (m_pMyEdit)
      {
            CRect rect(0, 0, lpCreateStruct->cx, lpCreateStruct->cy);
            AfxEnableControlContainer();
            BOOL IsCtrlCreated = m_pMyEdit->Create("Test", "TestWnd", WS_VISIBLE |
            WS_CHILD, rect, this, 101);
            if(!IsCtrlCreated )
            {
                  AfxMessageBox("Error: Failed to create the control!!") ;
                  return 0;
            }
      }
      else
      {
            delete m_pMyEdit;
            return -1;
      }
      
      
      return 0;
}

Try it out.
VinExpert
Thanks!  That gives us a fighting chance.

In the constructor of CTestCtrl, do you initialize m_pMyEdit to NULL? What initialized m_pMyEdit?  I think it's non-NULL, but also not pointing to a valid object.

The code you posted had this fragment:

 > if (m_pMyEdit)
 >
 >
 >   CRect rect(0, 0, lpCreateStruct->cx, lpCreateStruct->cy);
 >   BOOL IsCtrlCreated = m_pMyEdit->Create("Test", "TestWnd", WS_VISIBLE |
 > WS_CHILD, rect, this, 101);
 >   if(!IsCtrlCreated )
 >    AfxMessageBox("Error: Failed to create the control!!") ;
 >   return 0;
 >  }

Certainly, there must be an opening brace after the if() statement, right? Otherwise, this wouldn't compile. Is there other code you deleted from that section?

 > 4. The crash Message is:
 > <  Unhandled exception in IEXPLORE.EXE (TESTCTRL.OCX): 0x0000005: Access Violation  >

There was no additional sentence about "the code at [some address] tried to do [some thing]" ?

..B ekiM
Not that anyone needs my agreement, but...

I agree that this is most likely the m_pMyEdit member not being initialized to NULL in the contructor.  Also, remember that anytime you delete this member, you should re-assign it to NULL for the benefit of other code that tests the value of the pointer to see if it's valid.  I understand that deleting a pointer does not assign it to NULL when it is done.  To make this easy on myself, I created a macro called DELETE_SAFE that looks liks this:

#define DELETE_SAFE(ptr) \
   if (ptr)\
   {\
      delete ptr; \
      ptr = NULL;\
   }\

So in your code, to safely delete a pointer you merely call:

DELETE_SAFE(pSomePointer);

On another note...you *must* implement the OnDraw function in your control.  The simplest implementation of this is to call UpdateWindow on the member pointer.  For example,

if (m_pMyEdit && m_pMyEdit->m_hWnd)
    m_pMyEdit->UpdateWindow();

I have found that checking the m_hWnd member also ensures that CreateWindow member has been called on the CWnd-derived pointer.
ASKER CERTIFIED SOLUTION
Avatar of mikeblas
mikeblas

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kawa

ASKER

Thanks Mike.
It was the initialization of the pointer that I missed.
My problem is solved.

Thanks for anyone who tried to help.