Memory Leaks help

Ok here is a program im working on and I don't understand why there is memory leaks.

Main.cpp
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
 #define _CRTDBG_MAP_ALLOC
// -------------- SOLUTION CODE ------------------
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

#include "Stack.h"
#include "LabelledStack.h"

#define STACKSIZE 10

void tutorial() {
	cout << "----- TUTORIAL 9 - EASTER EGG ... ERR, MEMORY LEAK HUNT -----\n\n";
	
	// Make a couple of stacks
	Stack<int> * stack = new Stack<int>();
	Stack<int> * labelledStack = new LabelledStack<int>(STACKSIZE, "The Mighty Stack of int Pointers");

	// Generate some random numbers
	cout << "> generating some random numbers and stuffing them into an array\n\n";
	int ** numbers = new int*[STACKSIZE];
	for(int i=0; i<STACKSIZE; i++) {
		numbers[i] = new int(rand() % 100);
	}

	// Push some items on to the stack
	for(int i=0; !(stack->full()); i++) {
		stack->push((numbers[i]));
	}

	// Print out contents of the stack
	cout << "> stack\n" << *stack << endl;

	// Print out contents of the labelledStack
	cout << "> labelledstack\n" << *labelledStack << endl;

	// Empty the stack into labelledStack
	cout << "> popping off the stack and pushing into labelled stack\n\n";
	while(!(stack->empty())) {
		labelledStack->push(stack->peek());
		stack->pop();
	}

	// Print out contents of the stack
	cout << "> stack\n" << *stack << endl;

	// Print out contents of the labelledStack
	cout << "> labelledstack\n" << *labelledStack << endl;

	// Clean-up!
	delete stack, labelledStack;
	delete [] numbers;

	cout << "----- THE END ------\n";
	return;
}

int main() {
	tutorial();
	return 0;
}


Stack.h
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
#ifndef STACK_H
#define STACK_H

// Class Stack is a template for a bounded LIFO stack.
// Note that Stack only stores the pointers to its items (i.e. it is not "responsible" for its contents).

template<class T>
class Stack {
public:
	// Default constructor
	Stack(const int size = 10) : capacity(size), numberOfItems(0) {
		if(size < 1) {
			capacity = 10;
		}
		buffer = new T * [capacity];
	}

	// Destructor
	~Stack() {
	   delete [] buffer;
	}

	// Checks if the stack contains any elements
	bool empty() const {
		return (numberOfItems < 1);
	}

	// Checks if the stack is full
	bool full() const {
		return (numberOfItems == capacity);
	}

	// Pushes a pointer to an element onto the stack, returns itself
	Stack<T> & push(T * item) {
		if(numberOfItems < capacity) {
			buffer[numberOfItems++] = item;
		}
		return *this; 
	}

	// Pops an elements off the stack, returns itself
	Stack<T> & pop() {
		if(!empty()) {
			numberOfItems--;
		}
		return *this;
	}

	// Returns the pointer to the element at the top of the stack
	T * peek() const {
		if(!empty()) {
			return buffer[numberOfItems-1];
		}
		return NULL;
	}


	// Output the contents of the stack to a stream
	virtual void printOn(ostream & outstream) const {
		outstream << "Contents:\n";
		if(!empty()) {
			for(int i=0; i < numberOfItems; i++) {
				outstream << *(buffer[i]) << endl;
			}
		}
		else {
			outstream << "EMPTY\n";
		}
		return;
	}

private:
	T ** buffer;
	int capacity, numberOfItems;
};

template<class T>
ostream & operator<<(ostream & ostr, const Stack<T> & aName) {
	aName.printOn(ostr);
	return ostr;
};

#endif // STACK_H 


LabelledStack.h
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
#ifndef LABELLEDSTACK_H
#define LABELLEDSTACK_H

#include "Stack.h"

// (Derived) Class LabelledStack is a template for a bounded LIFO stack with a label.
// Note that Stack only stores the pointers to its items (i.e. it is not "responsible" for its contents).

template <class T>
class LabelledStack : public Stack<T> {
public:
	// Default constructor
	LabelledStack(const int size, string newLabel) : Stack<T>(size){
		this->label = new string(newLabel);
	}

	// Destructor
	~LabelledStack() {
		delete label;
	}

	// Output the contents of the stack to a stream
	void printOn(ostream & outstream) const {
		outstream << "Stack label: " << *label << endl;
		Stack<T>::printOn(outstream);
		return;
	}

private:
	string * label;
};

#endif // LABELLEDSTACK_H 
 
delete stack, labelledStack;

This does not do what you think. Only stack is deleted.
Last edited on
you're too trigger happy
http://www.cplusplus.com/forum/general/138037/
would say that the fix isn't to add delete labelledStack; but to avoid all those new in the first place.

also, it's not expoited in the code, but you are missing a proper copy constructor and assigment operator
http://en.cppreference.com/w/cpp/language/rule_of_three

edit: you're also missing a virtual destructor http://en.cppreference.com/w/cpp/language/destructor
je, your class is fun.
Last edited on
Topic archived. No new replies allowed.