We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now


Assert Error when deleting a CDialog Object

david_johns asked
Medium Priority
Last Modified: 2013-11-20
All of the sudden my code started giving me Assert Errors everytime I delete a CDialog object created with new.  It didn't used to do that and I can't figure out what has changed.  For example the following code gives me an Assert error on the delete line:

      CEditStringDlg *pEditStringDlg=NULL;
      pEditStringDlg = new CEditStringDlg;
      pEditStringDlg->DoModal("Testing...", "Testing...", &m_TempDirectory);
      delete pEditStringDlg;

Of course if I do not use delete the CDialog is dumped at program close.  Any ideas why this is giving me this error?

David Johns
Watch Question

Can u post the Assert Text...
AndyAinscowFreelance programmer / Consultant

Is there a reason why you dont do the following?

CEditStringDlg dlg(this);



The assert text is as follows:

File: dbgheap.c
Line: 1084

Expression: pHead->nBlockUse==nBlockUse

Sometimes I do.  The case I posted was just an example I tried when I started having problems.  The problem that brought my attention to it is that I am switching between 3 child dialogs in my main dialog and I create them in this manner and then delete them when another one is in use.  I suppose I could just have all three of them running and show or unshow them then delete them at the end, but I would still have this problem eventually.
AndyAinscowFreelance programmer / Consultant

But DoModal is to show a dialog once and have the user respond to it.  You seem to imply you call DoModal multiple times on it.
Unlock this solution and get a sample of our free trial.
(No credit card required)



No, acutally I am using create on the majority of my dialogs and running them modeless.  I just noticed the problem with my child windows and then tried this very simple testcase to see if the same problem occured on them and it seems to happen with every dialog I allocate using new.


I figured this was probably an overwrite, but it seemed wierd to me that it happens every I put it.  Actually I even tried it in a seperate project and got the same result.  I should try it with a completely clean project, but it really suprized me when it occured in both this project and a very very simple on that I also tried it in.


So - I tried it in a new project and played around a bit.  When I use new to declare it as an array it works.

    pDlg = new CTestDlg[1];
    delete[] pDlg;

But even in a completely clean project it doesn't work with my original approach - so it seems not to be heap corruption.  Anyone know why this happens?  Is this the only way to get this to work?  Perhaps this is how I got around it before, but I don't remember having to do this.

In the completely new project, did you change any other code? Did you add any code to the CTestDlg class?


Nope - The  test project was straight out of the app wizard.  I just put a bit in the initinstance that made the dialog using 'new' instead of creating it in local memory.  I left the dialog just as it comes out of appwizard and added no other functions.

Please show the InitInstance() code.


So this is embarassing but I tried it again and couldn't recreate the problem.  I looked into it more and figured out that I had actually launched a dialog I created in a static library I made rather than the CTestDlg.  I then looked into my library a bit and cleaned out some classes I was not using.  It worked then.  Either I had a bad build of the library or there was some problem with those classes.  Thanks everyone for the help.

David Johns
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.