gotlight
gotlight

Reputation: 88

How to avoid memory leaks in program with dynamical array containing pointers to objects?

I'm having trouble with memory usage in my homework program, which is used to store information about companies and its' owners. Class СompanyTemplate represents this info.

public:
CompanyTemplate (const string & oName,
                 const string & oAddr,
                 const string & cName,
                 const string & cAddr);

Another Class CCompanyIndex is used to store multiple records using a dynamical array of pointers (I'm not allowed to use vectors). Here's CCompanyIndex constructor:

CCompanyIndex :: CCompanyIndex (void)
{
allocated = 1000;
current_size = 0;
pole = new CompanyTemplate* [allocated];
for (int i=0; i<allocated; i++)
{
    pole[i] = NULL;
}
}

CCompanyIndex also provides methods Add (add record), Del (delete record), Search(search an information about owner's company. I'm having trouble with the Add method, although all basic tests are good, I have memory leaks, as valgrind says, in Add method.

bool CCompanyIndex :: Add( const string & oName,
                       const string & oAddr,
                       const string & cName,
                       const string & cAddr )
{

int pos = findPos(oName, oAddr);
        if(pos != -1)
        {
            return false;
        }
if ((current_size)>=allocated)
{
    CompanyTemplate ** temp;
    allocated = allocated*2+1;
    temp = new CompanyTemplate* [allocated];
    for (int i=0; i<current_size; i++)
    {
        temp[i]=pole[i];
    }
pole = temp;

for (int i=0; i<current_size; i++ )
{
    if ((pole[i])->Compare(oName,oAddr)<0)
    {
        current_size++;
        for (int k=current_size-1; k>=i; k--)
        {

        pole[i] = new Comp   pole[k+1]=pole[k];
        }anyTemplate(oName, oAddr, cName,cAddr);
        return true;
    }
}

pole[current_size] = new CompanyTemplate(oName, oAddr, cName,cAddr);
current_size++;
return true;
}

Array elements reallocating works as expected, more likely I have an error in destructor, but still can't find. Here it is:

CCompanyIndex :: ~CCompanyIndex (void)
{
for (int i=0; i<allocated; i++)
{
    delete pole[i];
}
delete [] pole;
pole = NULL;
}

Thanks

Upvotes: 1

Views: 452

Answers (2)

qPCR4vir
qPCR4vir

Reputation: 3571

With such a general title, the general answer will: use a vector of shared_ptr.

But I assumed that your homework is to implement a kind of std::vector<CompanyTemplate> using "low" level C++, witout STL and smart pointers (with otherwise is the best way of using c++). So:

Maybe you have other error, but here are two:

CompanyTemplate ** temp;
allocated = allocated*2+1;
temp = new CompanyTemplate* [allocated];
int i=0
for (; i<current_size; i++)
{
    temp[i]=pole[i];
}
for (; i<allocated ; i++)  // you want to make NULL the new pointers
{
    temp[i]=NULL
}
delete [] pole;    // delete old array.
pole=temp;

Upvotes: 1

Cheers and hth. - Alf
Cheers and hth. - Alf

Reputation: 145269

if ownership is unclear, just use std::shared_ptr.

of course in a professional setting a better answer might be to analyze better and get a better idea of ownership, like, is it really shared?

but absent that, use a std::shared_ptr.


by the way, it looks like your class fails to handle copying properly. this is called the "rule of three" (or for c++11, the "rule of five"). essentially, if you define either of destructor, copy constructor or copy assignment operator, then you most likely need all three in order to deal properly with copying.

but the simplest is to not define such operations but instead use standard library containers, like std::vector, and standard library smart pointers, like std::shared_ptr.

e.g. instead of defining pole as a raw pointer (to array), define it as a std::vector.

Upvotes: 1

Related Questions