Hello all,
I am a bit of a C++ newbie, so I apologize if this is a simple question.
I have a child class that creates a dynamic array of objects of another class.
The header file uses the following to define the pointer variable:
Passenger * passengerList;
The Constructor for the class is allocating the memory for the array as follows:
passengerList = new Passenger[10];
And finally the destructor has the following code to de-allocate the array:
delete [] passengerList;
passengerList = NULL;
This allocation of memory is producing a memory leak and I can't figure out why. I have stepped through the code, tracked the allocation of the array and watched it pass through the destructor and supposedly get de-allocated. And yet I still have the memory leak. Is there any reason why a delete wouldn't work. Oh, and no there aren't any errors during execution.
Thanks
Christian, I double checked my
passenger
class and there aren't any pointers used in it. Also, I'm using the following for my leak detection.
This is included:
#ifdef _WIN32
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#endif
Followed with this function call in Main():
#ifdef _WIN32
_CrtDumpMemoryLeaks();
#endif
I read that this is a proper method, but as I pointed out I am a newbie and am open to better methods.
ASPDOTNETDEV, here is the function that actually uses the array:
const void MagicSchoolBus::addPassenger(const Passenger& p)
{
int pc = this->GetPassengerCount();
int mp = this->GetMaxPassengers();
if (!this->GetRunningState())
{
if (pc < mp)
{
passengerList[pc] = p;
this->SetPassengerCount(++pc);
cout << "passenger added" << endl;
}
else
{
mp = mp + 10;
Passenger * tempList = new Passenger[mp];
for (int i = 0; i < pc; i++)
{
tempList[i] = passengerList[i];
}
delete [] passengerList;
passengerList = tempList;
this->SetMaxPassengers(mp);
passengerList[pc] = p;
this->SetPassengerCount(++pc);
cout << "passenger added" << endl;
}
}
else
{
cout << "Cannot add passengers while the vehicle is running" << endl;
}
}
As one might have already guessed, this is for a programming course I am taking. The project has already been turned in because the due date has come and gone but I refuse to let this program beat me and I am determined to find out what is causing the leak.
Thanks again for the suggestions!
Thanks for the continued help everyone. Based on Aescleal's leak tracking suggestion I am getting the same leaks reported that I showed before so apparently I really do have the leaks.
As requested I have added the Passenger constructors and destructor here:
Passenger::Passenger(void)
{
passengerWeight = 0;
strcpy_s(passengerName, "Default Name");
}
Passenger::Passenger(char pName[], int pWeight)
{
passengerWeight = pWeight;
strcpy_s(passengerName, pName);
}
Passenger::~Passenger(void)
{
}
Also, here are the complete constructors and destructor for the MagicSchoolBus class:
MagicSchoolBus::MagicSchoolBus(void) : Car(0, 10, "Default MagicSchoolBus")
{
Vehicle::SetMaxPassengers(10);
passengerList = new Passenger[10];
for (int i = 0; i < 10; i++)
{
passengerList[i] = Passenger();
}
}
MagicSchoolBus::MagicSchoolBus(int fCap, int fRate, char name[MAXIMUM_NAME_SIZE]) : Car(fCap, fRate, name)
{
Vehicle::SetMaxPassengers(10);
passengerList = new Passenger[10];
for (int i = 0; i < 10; i++)
{
passengerList[i] = Passenger();
}
}
MagicSchoolBus::~MagicSchoolBus(void)
{
delete [] passengerList;
passengerList = NULL;
}
Finally here is how the objects are created in Main():
MagicSchoolBus m1;
MagicSchoolBus m2(50, 5, "Craig's Bus");
Also an interesting point; m1 is the only object that has a memory leak. The m2 object does not, even though the contructors are identical.
Thanks again everyone.
Okay here is another data point and I apologize for not mentioning this before. I also get a memory leak that occurs when the passengerList array is copied during the addPassenger function. Previously, the m2 object wasn't presenting as the source of a memory leak because I think its memory gets destroyed when addPassenger creates the new passengerList and the new passengerList becomes the leak.
I came to this conclusion when I removed the m2.addPassenger calls from Main. When I did this suddenly m2 became the source of a memory leak in addition to m1's leak. I also tried adding enough passengers to m1 to force the creation of a new array and it removed the original m1 leak and created (or transfered depending on your interpretation) the leak to the new instance of the array.
I suspect some kind of scope issue but I'm not sure how to diagnose it.
Thanks
Aspdotnetdev,
As requested here are the SetPassengerCount and SetMaxPassengers functions.
const void Vehicle::SetPassengerCount(const int pc)
{
passengerCount = pc;
}
const void Vehicle::SetMaxPassengers(const int mp)
{
maxPassengers = mp;
}
As you can see they are very simple setters so I don't see how they could cause a problem. But then again I am the newb and not the expert; yet.
As for using:
this->SetPassengerCount(++pc);
Is using ++pc generally frowned upon from an industry perspective? Its one of those things I did because I know it works and wasn't necessarily thinking about any perceived poor programming practice.
Per your suggestion I tried setting tempList to NULL but it had no effect.
Thanks
I'm using Visual Studio 2008. I have zipped up the project and put it in the public folder of my Dropbox. It can be downloaded from here:
http://dl.dropbox.com/u/1262192/crgMustang_MemoryLeak_problem.zip[
^]
And I just want to apologize ahead of time to anyone who happens to look at this. Its not as clean as I like to have my code but I became obsessed with tracking down this memory leak and worked on it right up until my instructors deadline and wound up submitting it in this unfinished state.
But thanks to anyone who takes the time to look at it. It mean a lot to me to get this stuff right.