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





kawaAsked:
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.

mikeblasCommented:
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
0
kawaAuthor Commented:
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.

0
Vinayak KumbarSr Program ManagerCommented:
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
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

mikeblasCommented:
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
0
mandhjoCommented:
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.
0
mikeblasCommented:
I meant to submit my last post as an answer: calling through the invalid pointer is causing your crash.  You need to clean up your managemnet of the pointer.

This is certain, unless there's important code in the bit that you've deleted.

..B ekiM
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
kawaAuthor Commented:
Thanks Mike.
It was the initialization of the pointer that I missed.
My problem is solved.

Thanks for anyone who tried to help.
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.