How to fix memory leak?

The program runs and has no errors, except for a memory leak. I thought I deleted all the dynamic memory blocks but I can't see where I'm missing the deletes. All cases where new and delete are used in my code are below. Also I have to use dyn arrays for this project, even though vectors would be infinitely easier.

The arrays are first created and dyn memory comes into play
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
  void Zoo::animalInventory()
{
	
	tigers = new Tiger* [10]; //dynamic array of tiger objects
	tigerArraySize=10;
	for (int count=0; count<tigerArraySize; count++)
	{
		tigers[count]=NULL;
	}
	numTigers=0;

	penguins = new Penguin* [10]; //dynamic array of penguin objects
	penguinArraySize=10;
	for (int count=0; count<penguinArraySize; count++)
	{
		penguins[count]=NULL;
	}
	numPenguins=0;
	
	turtles = new Turtle* [10]; //dynamic array of turtle objects
	turtleArraySize=10;
	for (int count=0; count<turtleArraySize; count++)
	{
		turtles[count]=NULL;
	}
	numTurtles=0;

}

New animals are born or purchased
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
	if (type=="tiger")
	{
		newTiger(new Tiger(daysOld),num);
		if (daysOld>0)
		{
			subCost((tiger.getCost())*num);
			std::cout << num << " tiger(s) purchased (-$" << tiger.getCost()*num << ")! Bank Balance Remaining: $" << getBankBalance() << std::endl;
			
		}
	}
	if (type=="penguin")
	{
		newPenguin(new Penguin(daysOld),num);
		if (daysOld>0)
		{
			subCost((penguin.getCost())*num);
			std::cout << num << " penguin(s) purchased (-$" << penguin.getCost()*num << ")! Bank Balance Remaining: $" << getBankBalance() << std::endl;
		}
	}
	if (type=="turtle")
	{
		newTurtle(new Turtle(daysOld),num);
		if (daysOld>0)
		{
			subCost((turtle.getCost())*num);
			std::cout << num << " turtle(s) purchased (-$" << turtle.getCost()*num << ")! Bank Balance Remaining: $" << getBankBalance() << std::endl;
		}
	}
	if (type=="none")
	{
		menu.message("You did not purchase an animal at this time");
	}
}


Inventory size is doubled if births exceed size
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
void Zoo::increaseInventory(int type)
{
	int newTigerSize=tigerArraySize*2;
	int newPenguinSize=penguinArraySize*2;
	int newTurtleSize=turtleArraySize*2;
	
	if (type==1)
	{
		Tiger** newTigers = new Tiger* [newTigerSize]; //make new dyn array x2 the size
		for (int count=0; count<(newTigerSize); count++) //set elements to null
		{
			newTigers[count]=NULL;
		}
		for (int count=0; count<tigerArraySize; count++) //copy old array into new array
		{
			newTigers[count]=tigers[count];
		}
		delete [] tigers; //delete old array
		tigers=newTigers; //set member variable to new array
		tigerArraySize*=2; //update array size
	}
	else if (type==2)
	{
		Penguin** newPenguins = new Penguin* [newPenguinSize]; //make new dyn array x2 the size
		for (int count=0; count<(newPenguinSize); count++) //set elements to null
		{
			newPenguins[count]=NULL;
		}
		for (int count=0; count<(penguinArraySize); count++) //copy old array into new array
		{
			newPenguins[count]=penguins[count];
		}
		delete [] penguins; //delete old array
		penguins=newPenguins; //set member variable to new array
		penguinArraySize*=2; //update array size

	}
	else if (type==3)
	{
		Turtle** newTurtles = new Turtle* [newTurtleSize]; //make new dyn array x2 the size
		for (int count=0; count<(newTurtleSize); count++) //set elements to null
		{
			newTurtles[count]=NULL;
		}
		for (int count=0; count<(turtleArraySize); count++) //copy old array into new array
		{
			newTurtles[count]=turtles[count];
		}
		delete [] turtles; //delete old array
		turtles=newTurtles; //set member variable to new array
		turtleArraySize*=2; //update array size

	}
	
}


Memory is freed
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
void Zoo::deleteInventory()
{	
	for (int count=0; count<numTigers; count++)
	{
		delete tigers[count]; //delete each tiger object
		count++;
	}
	for (int count=0; count<numPenguins; count++)
	{
		delete penguins[count]; //delete each penguin object
		count++;
	}
	for (int count=0; count<numTurtles; count++)
	{
		delete turtles[count]; //delete each turtle object
		count++;
	}

	//delete emptied arrays
	delete [] tigers;
	tigers = NULL;

	delete [] penguins;
	penguins = NULL;

	delete [] turtles;
	turtles = NULL;
}
Hello stephcat5,

Starting you question with a green check tells everyone that you have an answer. Just so you know.

I believe you can edit the post and change the icon which will increase you chances of getting a response.

Andy
1
2
3
4
5
for (int count=0; count<numTigers; count++)
	{
		delete tigers[count]; //delete each tiger object
		count++;   //!! So, delete every other tiger then.
	}

Topic archived. No new replies allowed.