Array deletion, DEBUG ASSERTION FAILED error

Posted on 2003-03-14
Medium Priority
Last Modified: 2007-12-19
Hello, I am trying to build a menu object to use in a console mode program.
The constructor runs perfectly and sets up the object. The same thing happens with the following function. All the results (visible to me) are the ones expected.

My problems occurs when the Destructor function is called and the place where the error pops up will
be mentioned below .

Here is the class definition :
class WindowMenu
     WindowMenu  (int left, int right, int top, int NumberOfMenuEntries,...);
     void Initialize(void);
     SMALL_RECT  *ActiveSurface;
     SMALL_RECT   Client;
     int               NumberOfMenuEntries;
     char          *Entry[MAX_MENU_ENTRY_LENGTH];
     char         *MenuBuffer;
     int               BufferSize;
     int               BkColor;
     int               ForeColor;

void WindowMenu::Initialize(void)
     int Width = Client.Right - Client.Left + 1;
     int Height = Client.Bottom - Client.Top + 1;
     BufferSize = Width*Height;
     MenuBuffer = new char[BufferSize];
     ZeroMemory(MenuBuffer, sizeof(MenuBuffer));

     for(int i = 0; i< NumberOfMenuEntries; i++)
          strcat(MenuBuffer, Entry[i]);

Here I am just deleting the objects for which i have allocated memory.

WindowMenu::~WindowMenu(void) {

     delete[] Entry[MAX_MENU_ENTRY_LENGTH];  //works fine
     delete[] ActiveSurface; //works fine
     delete[] MenuBuffer;    //OUCH!! ERROR :  DEBUG ASSERTION FAILED in file dbgdel.c line 59
                                //                Expression : _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)  

What causes this error and how could I fix it?

Thank You!
Question by:avramescu
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3

Author Comment

ID: 8140671
Sorry about the low points, It's all I have.

Accepted Solution

violent1 earned 120 total points
ID: 8141581
well, let's see and take it slow [well, nobody likes to go slow...but come on...better safe than sorry, right? ;]

anyway.  MenuBuffer is just a pointer to char.  no problem there.  then you go and "new" it.  looks good to me.  you then fill it using strcat, right?  looks good.  just on a glance over and not having the program in front of me, i would agree and say that your program so far is working like you want.

so we move into the destructor.  die destructor!  debug assertion failed!  i hate those.  no, seriously, i do!  in my somewhat limited experience, when i have gotten such an error dealing with char* it has been because i was trying to delete something that wasn't there.  of course, that doesn't appear to be the case here...however, there isn't any reason you can't check, right?

for instance, i might:

if(MenuBuffer != NULL)
   delete [] MenuBuffer
   MenuBuffer = NULL;

if you would be willing to try that and see if that at least by passes your error for now, that would be great =]

honestly, that is about the only thing i can see right now, since it is but a loley char*


Expert Comment

ID: 8141584
and of course, i happen to forget a semicolon.  but i assume you will catch and correct that ;]
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 8141633
Thanks so much for your answer. While the checkm is very welcome at that point in the destructor, it always turns true, which is very much what I would predict. Also I was positive about this because I was printing the buffer, just for fun, and everything was OK.

However, I have discovered a little thing ( little thing but with a HUGE impact) that you did in the check :

delete[] MenuBuffer;
MenuBuffer = NULL;

So I went back, and revised my destructor, and made it to
have this form

     delete[] Entry[MAX_MENU_ENTRY_LENGTH];
     Entry[MAX_MENU_ENTRY_LENGTH] = NULL;   //newly
     delete[] ActiveSurface;
     ActiveSurface = NULL;                  //same here
          delete[] MenuBuffer;
          MenuBuffer = NULL;             //the check

And IT WORKS WONDERS! The error vanished which is indeed very inciting. I lost hours trying to fix that thing so I could move on...

Now if you have any idea of why the missing variable = NULL; after array deletion caused that fault, i'd be more than interested to hear it.

Thanks again!

Author Comment

ID: 8141638
I think I'll remove the check because I am certain that MenuBuffer Will never be void...Hmmm...It might be though, if I choose to set up a window without a menu...I'll test this to see how things go.

Expert Comment

ID: 8141698
yeah, you don't need the check if you know that the array will always be there.  it's just habit for me, since the times i have used char* there were times where it wasn't always initizialized [ADT's].

anything i say in regards to the error is purely conjecture and shouldn't be taken as fact or even educated [educated in that i did research, cause i didn't ;]

i have never acatually stepped through the delete through a debugger to see what is going on.  i have been told to do that by professors, so that i know what is happening.  but i mean...geez....lol.  to me, it doesn't seem like it is necessary.  but, if we think about it, perhaps it is.

it's all based around the idea of new NEEDING to be deleted.  once you have newed something, it gets that memory.  the idea of delete [i think] is that it clears that memory.  but UNTIL you set that variable to NULL, the chunk of memory is still referenced.  again, that alone shouldn't be much problem.  but, if we think about a common data type, like an int: what ever memory management is done for int, it's taken care of behind the scenes.  the instant we start using new, we put, in essence, in our hands the memory management.  and, naturally, if we don't follow the proper "etiquette" for that management...we should expect errors.

LOL.  i just typed all that :/ and it could be completely wrong.  but those are my thoughts on this issue anyway ;]

glad you got rid of the error!


Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

762 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