OK, I suppose this question may sound too silly, but memory-management (especially in C/C++) has never been my strong point and since it’s usually not a noticeable thing, I tend to overlook it. So, forgive me if all this sounds stupid. However, since my current project involves A LOT of data and processing, memory consumption easily goes above 2GB in a matter of seconds and it definitely slows the whole thing down, so… it’s time I started thinking of ways how to fix it.
So, here is my situation…
My principal class (of which I’m creating some millions of instances (which hopefully are automatically deleted, since they stop being used), so supposedly this is the culprit) is (roughly) this :
class MyClass
{
public:
// My Constructors
MyClass ();
MyClass (std::string param);
// My Destructor (still empty)
virtual ~MyClass ();
// Some methods
void methodA(std::string moves);
void methodB();
//----------------------
// My Variables
//----------------------
boost::array<int,64> arrA;
boost::array<unsigned int,13> arrB;
unsigned int key;
boost::array<int,3> arrC;
int argA;
int argB;
};
And this is (roughly – actual code has been altered) how the instances of the above class are created :
vector<MyClass*> SomeOtherClass::getListOfObjects()
{
vector<MyClass*> objects;
for (int i=0; i<MAX_OBJS; i++)
{
// Do some preparatory work
objects += new MyClass();
}
return objects;
}
And here’s how the results of the above function are being used :
void SomeOtherClass::doSth()
{
vector<MyClass*> objs = this->getListOfObjects();
int objsSize = objs.size();
for (int i=0; i<objsSize; i++)
{
MyClass* obj = objs[i];
// Do sth with obj
delete objs[i];
}
}
So, my question is :
-
What should I do in my destructor, so that when the object is not needed anymore and thus released, all of its “subcomponents” are released too? (e.g. the 2
boost::arrays) -
Is there anything wrong you notice with the approach above?
Please let me know if there’s anything else you need to know about my implementation…
Your class members do not look to be dynamically allocated, in which case you don’t need to explicitly
deleteanything in the destructor. If you happened to leave out some pointers to allocated memory, in this question, which you allocate withnew, you would need todeletethese in the destructor.Remember, if you
newyou need todelete, similarly withnew[]–delete[]. Barring the case of allocation to astd::unique_ptr.If your
MyClassobjects themselves are allocated on the heap withnew, then you would have todeletethem.p.s. if you are using C++11, you should probably use
std::arraynow.From your new code, it is obvious that whoever keeps the list returned from
getListOfObjects(), needs to calldeleteon each element when its to be destroyed. It is likely the destructor ofSomeOtherClass.Alternatively you can wrap the
MyClass*pointers within astd::unique_ptrorstd::shared_ptr(or any of the boost smart pointers which might be relevant here) which would then delete automatically when the vector holding them goes out of scope and is destroyed.If
doSthis accurate in its representation, and ensures that ALL instances ofMyClassgetdeleted, then this code seems fine, from a memory leak standpoint.