So I was working on a destructor for a vector of object pointers and I wrote up a little test program to be able to get rid of any memory leaks. I have 2 methods. the first is to loop through the vector's elements and delete them, and the second uses an iterator. both seem to free the memory and are pretty tough to break. Is there a benifite to using one method over the other? and why?
note, this is a very alive test program, there's a few lines that don't do anything and some leftovers from older tests. the functions I'm most interested are baddelete() and IterDelete();
Code:
/*
* ClassRoom.h
*
* Created on: Jan 20, 2012
* Author:
*/
#include <string.h>
#include <vector>
#include <fstream>
using namespace std;
#ifndef CLASSROOM_H_
#define CLASSROOM_H_
class ClassRoom{
public:
ClassRoom();
~ClassRoom();
void Pu........(string*,int,int);
void printIt();
void BadDelete();
void IterDelete();
void otherVec();
void PushFile(char *, char *);
private:
struct FileEntry{
char * fileName;
char * path;
};
struct Person{
string * name;
int * age;
};
vector<Person*> * theRoom;
vector<FileEntry * > * itsFileSeqPtr;
vector<Person*> * aClass;
vector<Person*> * anotherRoom;
vector<Person*> * roomThree;
ofstream * file;
};
#endif /* CLASSROOM_H_ */
Code:
/*
* VectorTest.cpp
*
* Created on: Jan 20, 2012
* Author:
*/
#include <iostream>
#include <string.h>
#include <vector>
#include "ClassRoom.h"
using namespace std;
ClassRoom::ClassRoom():
theRoom(new vector<Person*>(15)),
itsFileSeqPtr( new vector<FileEntry *>()),
file(NULL){
//aClass(new vector<Person*>(6)),
//anotherRoom(new vector<Person*>(7)),
//roomThree(new vector<Person*>(9)){
}
ClassRoom::~ClassRoom(){
BadDelete();
IterDelete();
}
void ClassRoom::BadDelete(){
cout <<"deleting "<< theRoom->size() << " elems: the room" << endl;
for(unsigned int i = 0; i<theRoom->size(); i++){
if(i < theRoom->size())
delete (*theRoom)[i];
}
//theRoom->clear();
delete theRoom;
cout << "destructed."<< endl;
}
void ClassRoom::IterDelete(){
cout <<"deleting "<< itsFileSeqPtr->size() << " elems: the file" << endl;
vector<FileEntry*>::iterator itFile;
for(itFile = itsFileSeqPtr->begin(); itFile != itsFileSeqPtr->end(); ++itFile){
delete[] (*itFile)->path;
delete[] (*itFile)->fileName;
delete (*itFile);
}
delete itsFileSeqPtr;
}
void ClassRoom::Pu........(string * name, int age, int elem){
Person * tempPerson = new Person;
tempPerson->name=name;
tempPerson->age= &age;
(*theRoom).at(elem)= new Person;
(*theRoom).at(elem)->name = name;
(*theRoom).at(elem)->age = &age;
delete tempPerson;
}
void ClassRoom::PushFile(char * name, char * path){
FileEntry * fileEntryPtr = new FileEntry;
char * swfFilePath("I\'m a string son");
fileEntryPtr->fileName = new char[strlen(name) +1];
fileEntryPtr->path = new char[strlen(path) +1];
strcpy(fileEntryPtr->fileName,name);
strcpy(fileEntryPtr->path,swfFilePath);
itsFileSeqPtr->push_back(fileEntryPtr);
//delete fileEntryPtr;
//delete swfFilePath;
}
void ClassRoom::otherVec(){
//itsFileSeqPtr
}
void ClassRoom::printIt(){
vector<Person*>::iterator it;
for(it = theRoom->begin(); it != theRoom->end(); ++it){
if(*it !=NULL){
cout <<"Personname::" << *((*it)->name) << endl;
}
}
vector<FileEntry*>::iterator itFile;
for(itFile = itsFileSeqPtr->begin(); itFile != itsFileSeqPtr->end(); ++itFile){
if(*itFile !=NULL){
cout <<"Filename::" << ((*itFile)->fileName);
cout <<" path::" << ((*itFile)->path) << endl;
}
}
}
void RunIt(){
ClassRoom kinder;
string tempy = "steve";
kinder.Pu........(&tempy,20,0);
string tempy2 = "john";
kinder.Pu........(&tempy2,25,1);
string tempy3 = "danny";
kinder.Pu........(&tempy3,30,6);
kinder.PushFile("dude","man");
kinder.PushFile("dudemeis","manny");
kinder.PushFile("dudey","manmeister");
kinder.PushFile("miesterdude","manman");
kinder.PushFile("dudeman","mandude");
kinder.printIt();
}
int main() {
int i =0;
while(i< 10){
RunIt();
//sleep(1);
cout << "____________________________________" << endl;
i++;
}
cout << "end of main!" << endl;
}