Solved

MFC/C++: a hwnd related assertion failure is caused when I copy an existing dialog resource for another CDialog class

Posted on 2007-11-21
7
709 Views
Last Modified: 2013-12-14
Hello,

I've created a new CDialog derived class, which is similar to an existing one. I copied the existing Dialog resource for my new CDialog derived class, and added new objects for the dialog's widgets, which don't necessarily have the same name as the old objects.

The dialog executes, until I attempt to call any of the object's member functions, which causes various assertion/ensure failures:

This occurs when I attempt to call a CEdit's CWnd::SetWindowTextA(), for example:

void CWnd::SetWindowText(LPCTSTR lpszString)
{
      ENSURE(this);
      ENSURE(::IsWindow(m_hWnd) || (m_pCtrlSite != NULL)); // this is the offending line

      if (m_pCtrlSite == NULL)
            ::SetWindowText(m_hWnd, lpszString);
      else
            m_pCtrlSite->SetWindowText(lpszString);
}

I'm unsure what has gone wrong. Please help!

Regards,
Sternocera
0
Comment
Question by:sternocera
  • 3
  • 3
7 Comments
 
LVL 31

Expert Comment

by:Zoppo
ID: 20326880
Hi sternocera,

hard to say from that code fragment ...

You should first ensure that your code doesn't use any control-ID which isn't present in the copies resource.

Is there a call to SetWindowText in your dialog's class? Or is this called from another MFC function?

Maybe you could post some more code, especially the functions which call this above given code (from call-stack)?


regards,

ZOPPO
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20326886
Appears to be you have instantiated your dialog, but have not created, could you show the lines where you first use the object?
0
 

Author Comment

by:sternocera
ID: 20326910
Zoppo,

Here is the offending code:
void CEditExistingProduct::DoDataExchange(CDataExchange* pDX)
{
      CDialog::DoDataExchange(pDX);
      DDX_Control(pDX, IDC_SKU, SKUEdit); // IDC_SKU definitely exists in my dialog resource
...
      
}
...
BOOL CEditExistingProduct::OnInitDialog()
{
      CString sku_is; sku_is.Format("%i", Sku);
      //SKUEdit.SetWindowTextA(sku_is); // I have these commented out, so there is no assertion failure now
      //SKUEdit.EnableWindow(FALSE);
      
      return TRUE;
}

Incidentally, I'm sorry I didn't give you a fair share of the points that time, Zoppo. It was a stupid oversight on my part.

Thanks,
Sternocera


0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:sternocera
ID: 20326917
jaime_olivares,

This is how the CDialog derived object gets instantiated:

void CProductSearch::OnDoubleClickedRow(NMHDR* pNMHDR, LRESULT* pResult)
{
      int current = ProductReport.GetFirstSelectedItem();
      CString sku = ProductReport.GetItemText(current,1);
      int SkuInt = atoi(sku.GetBuffer(0));
      sku.ReleaseBuffer();
      
      CEditExistingProduct MyEdit;
      MyEdit.ChangeSku(SkuInt);
      MyEdit.DoModal();

}

Thanks,
Sternocera
0
 
LVL 31

Accepted Solution

by:
Zoppo earned 500 total points
ID: 20326923
Hi again,

no problem ...

Seems there's no exiting edit-box found with resource-ID 'IDC_SKU' in this dialog ...

Further you should call CDialog::OnInitDialog() within your CEditExistingProduct::OnInitDialog(), somehow like this:

BOOL CEditExistingProduct::OnInitDialog()
{
      CDialog::OnInitDialog();

      CString sku_is; sku_is.Format("%i", Sku);
      //SKUEdit.SetWindowTextA(sku_is); // I have these commented out, so there is no assertion failure now
      //SKUEdit.EnableWindow(FALSE);

      UpdateData( FALSE );      
      return TRUE;
}


Hope that helps,

ZOPPO
0
 

Author Comment

by:sternocera
ID: 20326938
Zoppo,

Including CDialog::OnInitDialog() in my function overload is something that I know I should have done, but forgot to.

Now that I have, no more assertion failures. Silly me.

Thanks a lot for your time,
Sternocera


0
 
LVL 31

Expert Comment

by:Zoppo
ID: 20326941
No problem, you're welcome ...
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Best IDE for PHP starters 11 134
noX challenge 17 126
SSRS troubles 4 68
PL SQL Developer 7 52
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question