I'm not expecting anyone to spend a lot of time, but I am running my program through something that detects memory leaks and am getting back that I am leaking a lot of memory. I can't figure out where or how or what to do to fix it.
Could someone give some tips for detecting and fixing memory leaks and / or possibly give my code a quick glance and just see if there are any obvious places it might be leaking memory. Any advice would help!
int main() {
//main arrays
Table *tables[100];
int tableCount=0;
//Waiter vars
Waiter waiters[100];
int numWtrs = 0; //Number of waiters so far
string waiterName;
string wtrTables;
//Menu and MenuItem vars
Menu myMenu = Menu();
string code;
string name;
double price;
Order *myOrder;
int count;
int value; //
string token;
Tokenizer str;
///////////////////////////////////////////////////////////////
////////////////////////CONFIGURATION///////////////////////////
string line;
ifstream config("config.txt");
if(config.is_open())
{
while(!config.eof()) {
getline (config,line);
///////////////////DETERMINE WHAT WE ARE READING
int headerType;//0 for Tables, 1 for Waiters, 2 for Menu, -1 for blank line
stringstream lineType(stringstream::in|stringstream::out);
lineType<<line;
string header;
lineType>>header;
if(header=="Tables:"||header=="Waiters:"||header=="Menu:"||header=="") {
if(header=="Tables:")
headerType=0;
elseif(header=="Waiters:")
headerType=1;
elseif(header=="Menu:")
headerType=2;
else //read in empty line
headerType=-1;
}
////////////////////READ INPUT BASED ON TYPE
else {
stringstream setup(stringstream::in|stringstream::out);
setup<<line;
switch(headerType) {
case 0: //Table
//Input vars
int tableID;
int maxSeats;
//read vars
setup>>tableID;
setup>>maxSeats;
//Create a new table and put into object array
tables[tableID]=new Table(tableID,maxSeats);
tableCount++;
break;
case 1: //Waiters
//Read
setup>>waiterName;
setup>>wtrTables;
//Place new object in array
waiters[numWtrs] = Waiter(waiterName,wtrTables);
str=Tokenizer(wtrTables);
str.setDelimiter(",");
while ((token = str.next()) != "") {
value = atoi(token.c_str());
(*tables[value]).assignWaiter(&waiters[numWtrs]);
}
numWtrs++;
break;
case 2: //Menu
//Read in vars
setup>>code;
setup>>name;
setup>>price;
//Add to Menu Object
myMenu.addItem(MenuItem(code,name,price));
break;
}
}//else statement - means recording config non header lines
}
}
config.close();
///////////////////////////////////////////////////////////////
////////////////////////ACTIVITY///////////////////////////
ifstream activity("activity.txt");
int totalOrders=0;
if(activity.is_open()) {
while(!activity.eof())
{
getline(activity,line);
//Tokenize to get different tokens
//All commands start with T# with # refering to table number
Tokenizer act(line);
act.setDelimiter(" ");
string actTemp = act.next();
int tableNum = actTemp[1]-48; //convert char to int of table #
//Now find the command
actTemp = act.next();
char cmd = actTemp[0];
switch(cmd) {
case'P': //Assign party to a table
//Check if waiter has been assigned or if party has been assigned
if((*tables[tableNum]).hasWaiter()==1&&(*tables[tableNum]).hasParty()==0)
(*tables[tableNum]).partySeated((actTemp[1]-48)); //Assign a party to the table
break;
case'O':
free(myOrder);
myOrder=new Order(100);
actTemp = act.next();
count = 0;
while(actTemp!="") {
(*myOrder).addItem(myMenu.findItem(actTemp.substr(0,2)));
count++;
actTemp=act.next();
}
(*tables[tableNum]).partyOrdered(myOrder);
totalOrders++;
break;
case'S':
(*tables[tableNum]).partyServed();
break;
case'C':
(*tables[tableNum]).partyCheckout();
break;
}
}
}
activity.close();
///////////////////////////////////////////////////////////////
////////////////////////QUERIES///////////////////////////
int query=0;
int counts;
int tableChoice;
while(query!=6) {
cout<<"1. How many tables are occupied right now?"<<endl;
cout<<"2. How many open orders right now?"<<endl;
cout<<"3. How many orders have been processed so far?"<<endl;
cout<<"4. What is the average occupancy of particular table today?"<<endl;
cout<<"5. What are the top 3 popular entries today?"<<endl;
cout<<"6. Exit Queries"<<endl;
cout<<"Enter your query command: ";
cin>>query;
switch(query) {
case 1: //occupied tables
counts=0;
for(int i=1;i<=tableCount;i++) {
if((*tables[i]).hasParty()==1)
counts++;
}
cout<<endl;
cout<<"There are "<<counts<<" occupied tables."<<endl;
cout<<endl;
break;
case 2: //open orders
counts=0;
for(int i=1;i<=tableCount;i++) {
if((*tables[i]).openOrder()==1)
counts++;
}
cout<<endl;
cout<<"There are "<<counts<<" open orders."<<endl;
cout<<endl;
break;
case 3: //processed orders
counts=0;
for(int i=1;i<=tableCount;i++) {
if((*tables[i]).openOrder()==1)
counts++;
}
cout<<endl;
cout<<"There were "<<(totalOrders-counts)<<" processed orders."<<endl;
cout<<endl;
break;
case 4: //Avg occupancy of table
//Total number of people seated at table
//Divided by total number of parties seated at the table
cout<<"Enter a table for your query: ";
cin>>tableChoice;
if(tableChoice>0&&tableChoice<=tableCount)
{cout<<"Average occupancy: "<<((*tables[tableChoice]).avgOccup())<<" people"<<endl;}
else
cout<<"Table number not valid."<<endl;
break;
case 5: //Top 3 popular entries
myMenu.top3();
break;
}
}
return 0;
}