Valgrind mismatch frees NEED HELP QUICK

Hello all, trying to complete a project for school due midnight and have come across two Valgrind errors,

Don't mind the messy and unneccessary code, it's fine for now, this project is set in milestones, I can refactor it after down the road when I have more time on my hands.

What I need help with is my objects are going out of scope when getting passed by values into functions. When they leave the function they destruct and the pointers are lost (I reset the pointers back to null for now with some prolly terrible implementation, just to get by), then when the client code completes and calls the destructors they send errors.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  ==149088== Mismatched free() / delete / delete []
==149088==    at 0x4C2BB8F: operator delete[](void*) (vg_replace_malloc.c:651)
==149088==    by 0x4017F7: sdds::Menu::~Menu() (Menu.cpp:119)
==149088==    by 0x4012DC: main (ms1_MenuTester.cpp:26)
==149088==  Address 0x5a24e80 is 0 bytes inside a block of size 1 alloc'd
==149088==    at 0x4C2A593: operator new(unsigned long) (vg_replace_malloc.c:344)
==149088==    by 0x4016EB: sdds::Menu::setSafe() (Menu.cpp:79)
==149088==    by 0x4019E3: sdds::Menu::operator=(sdds::Menu const&) (Menu.cpp:179)
==149088==    by 0x40121D: main (ms1_MenuTester.cpp:80)
==149088==
==149088== Mismatched free() / delete / delete []
==149088==    at 0x4C2BB8F: operator delete[](void*) (vg_replace_malloc.c:651)
==149088==    by 0x4017F7: sdds::Menu::~Menu() (Menu.cpp:119)
==149088==    by 0x4012EB: main (ms1_MenuTester.cpp:87)
==149088==  Address 0x5a24e30 is 0 bytes inside a block of size 1 alloc'd
==149088==    at 0x4C2A593: operator new(unsigned long) (vg_replace_malloc.c:344)
==149088==    by 0x4016EB: sdds::Menu::setSafe() (Menu.cpp:79)
==149088==    by 0x401BD0: sdds::Menu::add(char const*) (Menu.cpp:250)
==149088==    by 0x4011A5: main (ms1_MenuTester.cpp:73)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
void Menu::setSafe() {
		m_title = new char;                                       <<<Menu.cpp:79
		m_title[0] = '\0';
		m_safe = true;
	}

	Menu::Menu(const char* title, int indent) {
		if (title != nullptr && title[0] != '\0') {

			//Initialize title;
			setTitle(title);

			//Intitialize indentation
			m_indentation = indent;

			//Initialize pointer array
			for (int i = 0; i < MAX_NO_OF_ITEMS; i++) {
				m_menuItem[i] = nullptr;
			}

			//Initialize Count
			m_menuCount = 0;
			m_safe = false;
		}
		else {
			//Intiliaze title
			setSafe();

			//Intiialize pointer array
			for (int i = 0; i < MAX_NO_OF_ITEMS; i++) {
				m_menuItem[i] = nullptr;
			}

			//Initialize indent and count
			m_indentation = 0;
			m_menuCount = 0;
		}
	}

Menu::~Menu() {
		//Deallocate title
		delete[] m_title;                                           <<<Menu.cpp:119
		m_title = nullptr;

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


		//Reset values
		m_menuCount = 0;
		m_indentation = 0;

		m_safe = true;
	}

Menu& Menu::operator=(const Menu& menu) {
		//Check if menu has title
		if (menu) {
			//Check if menu has items
			if (menu.m_menuCount > 0) {
				//Check if we have items
				if (m_menuCount > 0) {
					//Delete our items
					for (int i = 0; i < m_menuCount; i++) {
						delete m_menuItem[i];
					}
				}

				//and allocate for new items
				for (int i = 0; i < menu.m_menuCount; i++) {
					//Add item updates count aswell
					add(menu.m_menuItem[i]->m_item);
				}
			}
			else {
				//Delete our items
				for (int i = 0; i < m_menuCount; i++) {
					delete m_menuItem[i];
					m_menuItem[i] = nullptr;
				}

				//Reset count
				m_menuCount = 0;
			}

			//Copy indentation
			m_indentation = menu.m_indentation;

			//Clear our title
			delete[] m_title;

			//Set new title
			setTitle(menu.m_title);
		}
		else {
			//Set to safe state
			setSafe();                                      <<<Menu.cpp:179
			m_menuCount = 0;
			m_indentation = 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++;
			}
			m_safe = false;
		}
		else {
			//Set to safe state
			setSafe();                                     <<<<Menu.cpp:250

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


If you guys can solve why it is going out of scope when being passed like this for example
 
void testMenus(Menu m, Menu sub1, const Menu& sub2)

It would solve the problem and I could remove setSafe() which is causing all this mayhem, the two mismatch frees are coming from when these two menus (m and sub1) get wiped after coming out of scope, and i need to use setSafe() to reallocate to a safe state.

Any help is appreciated, even if I look like a chimpanzee smashing keys on a keyboard, cause I feel like one right now.

Cheers fellow programmers! Wish you all the best!
You don't need to start a new thread for every single new issue you find.
Line 2 says new char but line 42 says delete[].

Since m_title is an array of char, adjust line 2 to say new char[1] instead.

delete a pointer iff it was obtained with new;
delete[] a pointer iff it was obtained with new[];
Call free on a pointer iff it was obtained with malloc or calloc or realloc.
Topic archived. No new replies allowed.