1

I've a class named as "Menu", this class has an array of pointers to Objects of class "MenuItem". First three relevant lines of Menu class looks like:

class Menu
{
    MenuItem *items[5];

In the Constructor of Menu class I'm creating items as:

for(int i=0; i<nItems; i++)
{
    items[i] = new MenuItem(titles[i],...); //with all necessary parameters

In the destructor of Menu class I'm deleting items as:

~Menu()
{
for(int i=0; i<nItems; i++)
    delete items[i];

Problem: My problem is that when I call this destructor I got an Error Debug Assertion Failed!. Except this destructor Everything works perfectly fine. If I don't call this destructor there is no error. I want to know how to tackle this problem? I want to delete this array, so that my memory gets clean.

6
  • if nItems <= 5, then it looks OK, so the problem must be somewhere else Commented Aug 27, 2014 at 10:31
  • Yes in my program nItems=4 Commented Aug 27, 2014 at 10:33
  • 1
    But what is the assertion ? Commented Aug 27, 2014 at 10:37
  • I don't know anything about it! Commented Aug 27, 2014 at 10:40
  • Memory management is tricky; you probably didn't follow the Rule of Three correctly. Why not use std::vector and/or std::unique_ptr, which are specifically designed to manage memory correctly, rather than juggling pointers and hoping for the best? Commented Aug 27, 2014 at 10:49

2 Answers 2

3

It's hard to say what you problem is without more context (although I would bet on an issue with the copy constructor and/or the assignment operator, or with the wrong value for nItems).

A better way to manage those raw pointers, is to not have them in the first place.

Consider :

An array of smart pointers :

std::unique_ptr<MenuItem> items[5];

Or an std::array of smart pointers :

std::array<std::unique_ptr<MenuItem>, 5> items;

Or a vector<> of smart pointers :

std::vector<std::unique_ptr<MenuItem>> items;
Sign up to request clarification or add additional context in comments.

6 Comments

How will I initialize these vectors of MenuItem?
@AliMohyudin use items.push_back(std::unique_ptr<MenuItem>(new MenuItem(...))); Hope this helps.
oh okay! I'll have to change everything in my code just because of this! But any way I'm gonna give it a try!
@AliMohyudin no that much, you'll see. On the other end you dont need your destructor (nor the assignment operator) which saves you from more bugs.
One more question: Is not it necessary to remove data from this vector?
|
1

I see nothing wrong with the code you posted.

Although the assertion happens when the objects get destroyed, the obvious bug occurs elsewhere; at some point during the object's lifetime something got scribbled over.

A debug assertion of this kind does not mean "the bug happened now!". It means "the bug happened some time ago, and I just noticed it!", basically.

Look for the bug elsewhere in your code. There are a number of debugging tools that can instrument code, and attempt to identify undefined behavior, like stomping on uninitialized or unallocated memory.

3 Comments

Actually my program works absolutely fine without deleting the objects' array in destructor.
Some other part of your code is obviously causing memory corruption. That's what the debug assertion is telling you. If you do not explicitly deallocate the memory that was allocated for this object, the process simply terminates. There is no need to further allocate or deallocate memory from the heap, so this problem does not get caught. Unfortunately, ignoring a problem will not make it go away. If you ever have to add more code or logic to your program, if you ever need to change it, in some way, it's very likely you're going to hit this assertion anyway, immediately terminating it.
I found the bug, I didn't need to call a destructor for a simple object(of Menu class) (not a pointer to Menu class).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.