algoProg
algoProg

Reputation: 728

c++ valgrind double pointer delete for memory leak prevention

After reading: C++ Array of pointers: delete or delete []? (reply by shai vashid) and http://www.cplusplus.com/forum/beginner/6651/

I implemented following:

Kernel.h

unsigned int **ConfigMeM;
//.....
~Kernel(){ //destructor
    for (unsigned int i=0; i<MeMSize; i++)
        delete [] MeM[i]; //Valgrind:- Invalid read of size 4
                                       - Invalid free() / delete / delete[] / realloc()
    delete [] MeM; //Valgrind: Invalid free() / delete / delete[] / realloc()

    for (unsigned int i=0; i<_item_tot; i++)
        delete [] ConfigMeM[i]; //Valgrind: Same comments as above
    delete [] ConfigMeM; //Valgrind: same as above
};

Kernel.cpp

//.......
MeM = new unsigned int*[MeMSize];
for (unsigned int i=0; i<MeMSize; i++){
    MeM[i] = new unsigned int[11]; //Valgrind: 14,608 bytes in 332 blocks are definitely lost in loss record 2,021 of 2,036
}
for (unsigned int i=0; i<MeMSize; i++){
    for (int j=0; j<10; j++){
        MeM[i][j] = 0;
    }
}
 //.....
 ConfigMeM = new unsigned int*[_item_tot];
for (unsigned int i=0; i<_item_tot; i++){
    ConfigMeM[i] = new unsigned int[3]; //Valgrind: 1,200 bytes in 100 blocks are definitely lost in loss record 1,131 of 2,036 
}
for (unsigned int i=0; i<_item_tot; i++){
    for (int j=0; j<3; j++){
        ConfigMeM[i][j] = 0;
    }
}
  //.....

I am not sure what I am doing wrong.

Any suggestions please?

Thank you.

Upvotes: 0

Views: 1875

Answers (1)

Mike Seymour
Mike Seymour

Reputation: 254651

My best guess is that an object your class is being copied using the implicitly-generated copy constructor or copy-assignment operator. This would leave two objects with pointers to the same memory; both would try to delete this memory on destruction, resulting in double-delete errors.

The easiest fix is to prevent copying by deleting these functions:

Kernel(Kernel const&) = delete;
void operator=(Kernel const &) = delete;

or, if you're stuck with an ancient compiler, declare them private with no function body.

If you need to copy these objects, then you will need to implement these, perhaps to perform a "deep copy" that allocates new blocks of memory.

Alternatively, it might be easier to use std::vector to manage your dynamic arrays; this already has correct copy semantics, and can be initialised quite simply:

std::vector<std::vector<unsigned int>> MeM, ConfigMem;

(MemSize, std::vector<unsigned int>(11));

Upvotes: 1

Related Questions