Keep getting memory leaks

Hello everyone! I'm writing this assignment for my project at my school, and I keep getting memory leaks but can't figure out why... I seem to have deleted every "new-command"... Am I missing something?

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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
main.cpp

#include "Teacher.h" 
#include "Assistant.h" 
#include "TA.h" 
#include "Employee.h"
#include <stdio.h>
#include <crtdbg.h>
#include <string>
#include <iostream>
#include <sstream>
 
using namespace std; 

int menu()
{
	cout << "\n0. Quit\n1. New assistent\n2. New TA-personel\n3. New teacher\n4. Print all\n\nInput: ";
	int choice=0;
	cin >> choice;
	cin.ignore();
	return choice;
}

Employee** addEmployee(Employee** arr, Employee* e, int &nrOfEmployees)
{
	nrOfEmployees++;
	//cout << "\n\n" << nrOfEmployees << "\n\n";
	Employee** temp = new Employee*[nrOfEmployees];
	for(int i=0;i<nrOfEmployees-1;i++)
	{
		temp[i] = arr[i];
	}
	temp[nrOfEmployees-1] = e;
	delete[] arr;
	arr = new Employee*[nrOfEmployees];
	for(int i=0;i<nrOfEmployees;i++)
	{
		arr[i] = temp[i];
	}
	delete temp;
	return arr;
}

int main() 
{
	_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF|_CRTDBG_LEAK_CHECK_DF);
	int choice=0, nrOfEmployees=0;
	Employment* empl;
	Employee* e;
	Employee** personel = new Employee*[0];
	do
	{
		string name, position, mainSubject, studyProgram;
		int birthyear, wage, workingHours, pointsTaken; 
		bool isManager, isProgramManager, canCertify, hasSuperKey;
		choice = menu();
		switch(choice)
		{
		case 0:
			for(int i=0; i<nrOfEmployees;i++)
			{
				delete personel[i]->getposition();
			}
			delete[] personel;
			empl = NULL;
			e = NULL;
			delete empl;
			delete e;
			break;
		case 1:
		{
			cout << "Name: ";
			getline(cin,name);
			cout << "Birthyear: ";
			cin >> birthyear;
			cin.ignore();
			cout << "Position: ";
			cin >> position;
			cin.ignore();
			cout << "Wage: "; 
			cin >> wage;
			cin.ignore();
			cout << "Program studied: ";
			cin >> studyProgram;
			cin.ignore();
			cout << "Points taken: ";
			cin >> pointsTaken;
			cin.ignore();

			empl = new Assistant(position, wage, studyProgram, pointsTaken);
			e = new Employee(name, birthyear, empl);
			personel = addEmployee(personel, e, nrOfEmployees);
		}
			break;
		case 2:
		{
			cout << "Name: ";
			getline(cin,name);
			cout << "Birthyear: ";
			cin >> birthyear;
			cin.ignore();
			cout << "Position: ";
			cin >> position;
			cin.ignore();
			cout << "Wage: "; 
			cin >> wage;
			cin.ignore();
			cout << "Manager (1/0): ";
			cin >> isManager;
			cin.ignore();
			cout << "Working hours: ";
			cin >> workingHours;
			cin.ignore();
			cout << "Can certify (1/0): ";
			cin >> canCertify;
			cin.ignore();
			cout << "Has superkey (1/0): ";
			cin >> hasSuperKey;
			cin.ignore();

			empl = new TA(position, isManager, wage, workingHours, canCertify, hasSuperKey);
			e = new Employee(name, birthyear, empl);
			personel = addEmployee(personel, e, nrOfEmployees);
		}
			break;
		case 3:
		{
			cout << "Name: ";
			getline(cin,name);
			cout << "Birthyear: ";
			cin >> birthyear;
			cin.ignore();
			cout << "Position: ";
			cin >> position;
			cin.ignore();
			cout << "Wage: "; 
			cin >> wage;
			cin.ignore();
			cout << "Manager (1/0): ";
			cin >> isManager;
			cin.ignore();
			cout << "Working hours: ";
			cin >> workingHours;
			cin.ignore();
			cout << "Main subject: ";
			cin >> mainSubject;
			cin.ignore();
			cout << "Program manager (1/0): ";
			cin >> isProgramManager;
			cin.ignore();

			empl = new Teacher(position, isManager, wage, workingHours, mainSubject, isProgramManager);
			e = new Employee(name, birthyear, empl);
			personel = addEmployee(personel, e, nrOfEmployees);
		}
			break;
		case 4:
		{
			cout << endl;
			for(int i=0;i<nrOfEmployees;i++)
			{
				cout << "Employee " << i+1<< ":\n" << personel[i]->toString() << endl;
			}
		}
			break;
		default:
			break;
		}
	}while(choice!=0);

	return 0; 
} 

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
Employee.cpp

#include <stdio.h>
#include <fstream>
#include <string>
#include <iostream>
#include <sstream>
#include "Employee.h"
#include "Employment.h"

using namespace std; 

string Employee::toString()
{
	ostringstream ss;

	string sBirthYear = static_cast<ostringstream*>( &(ostringstream() << birthYear) )->str();

	ss << "Name: " << name << "\nBirthyear: " << sBirthYear << endl;
	return ss.str() + position->toString();
	delete ss;
}

int Employee::getWageType()
{
	Employment* temp = getposition();
	return temp->getWageType();
}

bool Employee::setname(string name)
{
	bool ok = true;
	if(name != "")
		this->name = name;
	else
		ok = false;
	return ok;
}

string Employee::getname()
{
	return this->name;
}

bool Employee::setbirthYear(int birthYear)
{
	bool ok = true;
	if(birthYear >=1900)
		this->birthYear = birthYear;
	else
		ok = false;
	return ok;
}

int Employee::getbirthYear()
{
	return this->birthYear;
}

void Employee::setposition(Employment* position)
{
	this->position = position;
}

Employment* Employee::getposition()
{
	return this->position;
}

Employee::~Employee()
{
	delete position;
}

Employee::Employee(string name, int birthYear, Employment* position)
{
	this->name = name;
	this->birthYear = birthYear;
	this->position = position;
}


Employee::Employee(const Employee& other)
{
	this->name=other.name;
	this->birthYear=other.birthYear;
	this->position=other.position;
}

Employee& Employee::operator=(const Employee& other)
{
	this->name=other.name;
	this->birthYear=other.birthYear;
	this->position=other.position;
	return *this;
}

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
Employment.cpp

#include <stdio.h>
#include <fstream>
#include <string>
#include <iostream>
#include <sstream>
#include "Employment.h"

using namespace std; 


string Employment::toString()
{
	ostringstream ss;
	string manager, wageType;

	string sWorkingHours = static_cast<ostringstream*>( &(ostringstream() << workingHours) )->str();
	string sWage = static_cast<ostringstream*>( &(ostringstream() << wage) )->str();

	if(this->isManager==true)
		manager="Yes";
	else
		manager="No";
	if(this->getWageType()==1)
		wageType=" / month";
	else
		wageType=" / hour";

	ss << "Position: " << this->typeName << "\nWage: " << sWage << wageType << "\nManager: " << manager << "\nWorking hours: " << sWorkingHours << " / year\n";

	return ss.str();
	delete ss;
}

bool Employment::settypeName(string name)
{
	bool ok = true;
	if(name != "")
		this->typeName = name;
	else
		ok = false;
	return ok;
}

string Employment::gettypeName()
{
	return this->typeName;
}

bool Employment::setisManager(bool b)
{
	this->isManager=b;
	return true;
}

bool Employment::getisManager()
{
	return this->isManager;
}

bool Employment::setwage(int wage)
{
	bool ok = true;
	if(wage >=1)
		this->wage = wage;
	else
		ok = false;
	return ok;
}

int Employment::getwage()
{
	return this->wage;
}

bool Employment::setworkingHours(int hours)
{
	bool ok = true;
	if(hours >=1)
		this->workingHours = hours;
	else
		ok = false;
	return ok;
}

int Employment::getworkingHours()
{
	return this->workingHours;
}

Employment::~Employment()
{

}

Employment::Employment(string typeName, bool isManager, int wage, int workingHours)
{
	this->typeName = typeName;
	this->isManager = isManager;
	this->wage = wage;
	this->workingHours = workingHours;
}

Employment::Employment(const Employment& other)
{
	this->typeName = other.typeName;
	this->isManager = other.isManager;
	this->wage = other.wage;
	this->workingHours = other.workingHours;
}

The memory leaks:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Detected memory leaks!
Dumping objects ->
{313} normal block at 0x007C5998, 8 bytes long.
 Data: < S|     > D0 53 7C 00 00 00 00 00 
{311} normal block at 0x007C53D0, 40 bytes long.
 Data: < Y| d           > 98 59 7C 00 64 00 CD CD CD CD CD CD CD CD CD CD 
{286} normal block at 0x007C5530, 8 bytes long.
 Data: < W|     > C8 57 7C 00 00 00 00 00 
{284} normal block at 0x007C57C8, 40 bytes long.
 Data: <0U| d           > 30 55 7C 00 64 00 CD CD CD CD CD CD CD CD CD CD 
{189} normal block at 0x007C54E8, 8 bytes long.
 Data: < O|     > E8 4F 7C 00 00 00 00 00 
{187} normal block at 0x007C4FE8, 40 bytes long.
 Data: < T| d           > E8 54 7C 00 64 00 CD CD CD CD CD CD CD CD CD CD 
Object dump complete.
The program '[8760] c++_lab4.exe: Native' has exited with code 0 (0x0).
Topic archived. No new replies allowed.