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.

quantdev
  • 23,517
  • 5
  • 55
  • 88
Ali Mohyudin
  • 242
  • 2
  • 10

2 Answers2

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;
quantdev
  • 23,517
  • 5
  • 55
  • 88
  • How will I initialize these vectors of MenuItem? – Ali Mohyudin Aug 27 '14 at 18:30
  • 1
    @AliMohyudin use `items.push_back(std::unique_ptr(new MenuItem(...)));` Hope this helps. – quantdev Aug 27 '14 at 18:31
  • 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! – Ali Mohyudin Aug 27 '14 at 18:36
  • @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. – quantdev Aug 27 '14 at 18:38
  • One more question: Is not it necessary to remove data from this vector? – Ali Mohyudin Aug 27 '14 at 18:44
  • 1) Yes, you can see also [this page](http://en.cppreference.com/w/cpp/memory/unique_ptr) fore more info. 2) Right, you dont need too, `std::vector` and `std::unique_ptr` will do the job for you. – quantdev Aug 27 '14 at 18:45
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.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • Actually my program works absolutely fine without deleting the objects' array in destructor. – Ali Mohyudin Aug 27 '14 at 18:19
  • 1
    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. – Sam Varshavchik Aug 27 '14 at 20:39
  • 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). – Ali Mohyudin Aug 28 '14 at 02:50