Exception thrown while deleting a pointer

  • Thread starter Thread starter Noah Pereira
  • Start date Start date
N

Noah Pereira

Guest
Hi Fellow Programmers!

I am having some trouble with this small project.

I am getting a exception thrown at me from "delete_scalar.cpp:Line 34: _free_dbg(block, _UNKNOWN_BLOCK);"

Which stems from me trying to delete a pointer holding a char array.

Here is context

The Destructor

Menu::~Menu() {
//Deallocate title
delete[] m_title;

//Deallocate menu items
if (m_menuCount > 0) {
for (int i = 0; i < m_menuCount; i++) {
delete m_menuItem;
m_menuItem = nullptr;
}
}


//Initialize values
m_menuCount = 0;
m_indentation = 0;

}

Here is the declarations of the objects

Menu mainMenu("** Main Menu **");
Menu subMenu1("** Sub Menu One **", 1);
Menu subMenu2("** Sub Menu **", 2);
Menu tempMenu("** Temp **");
Menu invMenu("** To test Invalid Menu **");

When the client code completes and the destructors start deleting the pointers, once the destructor hits subMenu2 it throws the exception, invMenu and tempMenu destruct first and no errors there.

Here is the values right at return 0, before destructors get called.

- invMenu {m_title=0x009c5248 "" m_menuItem=0x0077f718 {0x00000000 <NULL>, 0x00000000 <NULL>, 0x00000000 <NULL>, ...} ...} sdds::Menu
- m_title 0x009c5248 "" char *
0 '\0' char
+ m_menuItem 0x0077f718 {0x00000000 <NULL>, 0x00000000 <NULL>, 0x00000000 <NULL>, 0x00000000 <NULL>, 0x00000000 <NULL>, ...} sdds::MenuItem *[11]
m_menuCount 0 int
m_indentation 0 int
- mainMenu {m_title=0x009c8f78 "" m_menuItem=0x0077f818 {0x009cfeb8 {m_item=0xdddddddd <Error reading characters of string.> }, ...} ...} sdds::Menu
- m_title 0x009c8f78 "" char *
0 '\0' char
+ m_menuItem 0x0077f818 {0x009cfeb8 {m_item=0xdddddddd <Error reading characters of string.> }, 0x009d0128 {m_item=...}, ...} sdds::MenuItem *[11]
m_menuCount 0 int
m_indentation 0 int
- subMenu1 {m_title=0x009c6200 "" m_menuItem=0x0077f7d8 {0x009cffa8 {m_item=0xdddddddd <Error reading characters of string.> }, ...} ...} sdds::Menu
- m_title 0x009c6200 "" char *
0 '\0' char
+ m_menuItem 0x0077f7d8 {0x009cffa8 {m_item=0xdddddddd <Error reading characters of string.> }, 0x009d0188 {m_item=...}, ...} sdds::MenuItem *[11]
m_menuCount 0 int
m_indentation 0 int
- subMenu2 {m_title=0x009c9258 "** Sub Menu **" m_menuItem=0x0077f798 {0x009d0158 {m_item=0x009ce298 "The first" }, ...} ...} sdds::Menu
- m_title 0x009c9258 "** Sub Menu **" char *
42 '*' char
+ m_menuItem 0x0077f798 {0x009d0158 {m_item=0x009ce298 "The first" }, 0x009cffd8 {m_item=0x009ce7a0 "The second" }, ...} sdds::MenuItem *[11]
m_menuCount 3 int
m_indentation 2 int
- tempMenu {m_title=0x009d1de0 "** Sub Menu **" m_menuItem=0x0077f758 {0x009d0008 {m_item=0x009ce6f8 "The first" }, ...} ...} sdds::Menu
- m_title 0x009d1de0 "** Sub Menu **" char *
42 '*' char
+ m_menuItem 0x0077f758 {0x009d0008 {m_item=0x009ce6f8 "The first" }, 0x009cfee8 {m_item=0x009ce650 "The second" }, ...} sdds::MenuItem *[11]
m_menuCount 3 int
m_indentation 2 int


Apologizes I cant post images yet....

Also the only time subMenu2 gets interacted with is when adding menu options here

subMenu2 << "The first" << "The second" << "The third";

and the code for the adding here

Menu& Menu::operator<<(const char* item) {
if (*this) {
if (item != nullptr && item[0] != '\0') {
//Add the item
add(item);
}
else {
//Set title to safe state
m_title[0] = '\0';
}
}

//Return object
return *this;
}

void Menu::add(const char* item) {
if (item != nullptr && item[0] != '\0') {
//Check we are valid and are not exceeding max
if (m_menuCount < MAX_NO_OF_ITEMS && *this) {
m_menuItem[m_menuCount] = new MenuItem(item);
m_menuCount++;
}
}
else {
//Set to safe state
m_title[0] = '\0';

//Reset values, if title is nullptr then items have been nulled
m_menuCount = 0;
m_indentation = 0;
}
}

Any help would be appreciated, it's confusing because the values show its a normal pointer pointing to a char array identical to invMenu().


Thanks in advances, best wishes!

Continue reading...
 
Back
Top