Memory Leak Check

Just want to make sure this isn't producing a memory leak. I think I dropped it with the delete *it; in CMenu::menu()

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
void CMenu::newLoan(){
	int sel;
	CMortgage * mort;

	cout << "Enter the type of loan:" << endl;
	cout << "1. Simple Interest" << endl;
	cout << "2. Rule of 72" << endl;
	cout << "3. Fixed Payment" << endl;
	cout << "$ ";
	cin >> sel;
	switch(sel){
		case 1: // Allocating new CSimple: public CMortgage
			mort = new CSimple;
			mort->getValues();
			mort->print();
			morts.push_back(mort);
			break;
		case 2: // Allocating new CSeventyTwo: public CMortgage
			mort = new CSeventyTwo;
			mort->getValues();
			mort->print();
			morts.push_back(mort);
			break;
		case 3: // Allocating new CFixed: public CMortgage
			mort = new CFixed;
			mort->getValues();
			mort->print();
			morts.push_back(mort);
			break;
		default:
			cout << "That is not a valid choice!" << endl;
			newLoan();
		}
}

int CMenu::menu(){
	int sel;

	cout << "Enter your choice:" << endl;
	cout << "1. New Loan" << endl;
	cout << "2. Delete Loan" << endl;
	cout << "3. Print Loan" << endl;
	cout << "4. Edit Loan" << endl;
	cout << "5. Exit" << endl;
	cout << "$ ";
	cin >> sel;
	switch(sel){
		case 1: // Start up a new mortgage
			newLoan();
			break;
		case 2: // Page iterator to (class scope) list item and delete
			selLoan();
			delete *it;
			morts.erase(it);
			it = morts.begin();
			break;
		case 3: // Page iterator to (class scope) list item and print
			selLoan();
			(*it)->print();
			break;
		case 4: // Page iterator to (class scope) list item and edit
			selLoan();
			editLoan();
			break;
		case 5: // Return to int main()
			return 0;
			break;
		default:
			cout << "That is not a valid choice!" << endl;
		}
	menu();
}
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
void CMenu::newLoan(){
	int sel;
	CMortgage * mort;

	cout << "Enter the type of loan:" << endl;
	cout << "1. Simple Interest" << endl;
	cout << "2. Rule of 72" << endl;
	cout << "3. Fixed Payment" << endl;
	cout << "$ ";
	cin >> sel;
	switch(sel){
		case 1: // Allocating new CSimple: public CMortgage
			mort = new CSimple;
			mort->getValues();
			mort->print();
			morts.push_back(mort);
                        delete mort; 
			break;
		case 2: // Allocating new CSeventyTwo: public CMortgage
			mort = new CSeventyTwo;
			mort->getValues();
			mort->print();
			morts.push_back(mort);
                        delete mots
			break;
		case 3: // Allocating new CFixed: public CMortgage
			mort = new CFixed;
			mort->getValues();
			mort->print();
			morts.push_back(mort); 
                        delete mort;    

			break;
		default:
			cout << "That is not a valid choice!" << endl;
			newLoan();
		}
}

int CMenu::menu(){
	int sel;

	cout << "Enter your choice:" << endl;
	cout << "1. New Loan" << endl;
	cout << "2. Delete Loan" << endl;
	cout << "3. Print Loan" << endl;
	cout << "4. Edit Loan" << endl;
	cout << "5. Exit" << endl;
	cout << "$ ";
	cin >> sel;
	switch(sel){
		case 1: // Start up a new mortgage
			newLoan();
			break;
		case 2: // Page iterator to (class scope) list item and delete
			selLoan();
			delete *it;
			morts.erase(it);
			it = morts.begin();
			break;
		case 3: // Page iterator to (class scope) list item and print
			selLoan();
			(*it)->print();
			break;
		case 4: // Page iterator to (class scope) list item and edit
			selLoan();
			editLoan();
			break;
		case 5: // Return to int main()
			return 0;
			break;
		default:
			cout << "That is not a valid choice!" << endl;
		}
	menu();
}


please define the class of which morts is the object .
Last edited on
I'm not sure but I really dont think that delete *it makes any sense at all. Unless it is a double pointer ** then dereferencing it makes no sense to me
Last edited on
morts is the list. mort is the object of class CMortgage:
1
2
3
4
5
6
7
8
9
10
11
12
class CMortgage{
private:
	string fName, lName;
	double loanVal, payAmt, interest;
	int matTerm;
public:
	void getValues();
	void print();
	void printName();
	virtual void pmtCalc() =0;
	virtual void amortize() =0;
};


Perhaps you're also looking for:
1
2
3
4
5
6
7
8
9
class CMenu{
	vector<CMortgage *> morts;
	vector<CMortgage *>::iterator it;
public:
	void newLoan();
	void selLoan();
	void editLoan();
	int menu();
};
Last edited on
I'm not sure but I really dont think that delete *it makes any sense at all. Unless it is a double pointer ** then referencing it makes no sense to me 


It's an iterator to a list of pointers for an abstract class.
If you want to be sure, you could add a debug instance count to each of your classes and check it gets back to zero by the end.
closed account (1yR4jE8b)
valgrind?
$ valgrind ./a.out

==25978== 
==25978== HEAP SUMMARY:
==25978==     in use at exit: 0 bytes in 0 blocks
==25978==   total heap usage: 34 allocs, 34 frees, 1,190 bytes allocated
==25978== 
==25978== All heap blocks were freed -- no leaks are possible
==25978== 
==25978== For counts of detected and suppressed errors, rerun with: -v
==25978== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 4 from 4)
Topic archived. No new replies allowed.