Solved

Assert Error when deleting a CDialog Object

Posted on 2004-03-22
11
498 Views
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?

Thanks,
David Johns
0
Comment
Question by:david_johns
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 10647395
Can u post the Assert Text...
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 10647729
Is there a reason why you dont do the following?

CEditStringDlg dlg(this);
dlg.DoModal(...);
0
 

Author Comment

by:david_johns
ID: 10650799
Roshmon,

The assert text is as follows:

File: dbgheap.c
Line: 1084

Expression: pHead->nBlockUse==nBlockUse


AndyAinscow,
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.
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 44

Expert Comment

by:AndyAinscow
ID: 10651141
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.
0
 
LVL 23

Accepted Solution

by:
chensu earned 200 total points
ID: 10651149
This assertion failure means that the heap has got corrupted. You may have overwritten memory that does not belong to you. If it consistenly happens there, you can check the address of pHead->nBlockUse and watch the memory while stepping thru the code to see where in you code overwrites it.
0
 

Author Comment

by:david_johns
ID: 10651437
AndyAinscow,

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.

Chensu,

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.
0
 

Author Comment

by:david_johns
ID: 10651737
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.

Ex:
    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.
0
 
LVL 23

Expert Comment

by:chensu
ID: 10651927
In the completely new project, did you change any other code? Did you add any code to the CTestDlg class?
0
 

Author Comment

by:david_johns
ID: 10652944
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.
0
 
LVL 23

Expert Comment

by:chensu
ID: 10653692
Please show the InitInstance() code.
0
 

Author Comment

by:david_johns
ID: 10655501
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
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.NET how to use the Vertical ScrollBar 5 103
Tab names are off by one 5 54
modThree challenge 4 90
changeXy challenge 13 81
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

786 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