Search code examples
c++objectvectormemory-leaksdelete-operator

Free memory from vector of objects inside a function


I have memory leaks inside my code but I couldn't figure out a solution to free the memory allocated inside the function where the object is created and pushed into a vector of an object.

The main function is the following:

void foo(vector<vector<BCC>> &features){

    vector<MinutiaPair*> matchingMtiae;

    for (int i = 0; i < features.size(); i++){

        Match(features[0], features[i], matchingMtiae);
        ms += s;

        // Free memory
        for (int j = 0; j < matchingMtiae.size(); j++)
            delete (matchingMtiae[j]);

        matchingMtiae.clear();

}

Each step of the loop a comparison is executed between values and a "new" vector matchingMtiae is returned with new objects. Then, for the next iteration, I want to completely free this vector and deallocate its content from memory. The Match function where objects are created and pushed into a vector matchingMtiae is presented below:

void Match(vector<BCC> &qt, vector<BCC> &tt, vector<MinutiaPair*> &reducedMatchingPairs) {

    vector<MinutiaPair*> localMatching;

    for (int i = 0; i < qt.size(); i++)
        for (int j = 0; j < tt.size(); j++)
        {
            double currSim = qt[i].Match(tt[j], true);  
            if (currSim > 0)
            {
                 qt[i].minutia.Flag = false;
                 tt[j].minutia.Flag = false;
                 MinutiaPair *pair = new MinutiaPair(qt[i].minutia, tt[j].minutia, currSim);
                 localMatching.push_back(pair);
            }


    sort(localMatching.begin(), localMatching.end(), MtiaPairComparer::ComparePointers);


    for (int k = 0; k < localMatching.size(); k++)
    {
        if (!localMatching[k]->QueryMtia->Flag || !localMatching[k]->TemplateMtia->Flag)
        {
            reducedMatchingPairs.push_back(localMatching[k]);
            localMatching[k]->QueryMtia->Flag = true;
            localMatching[k]->TemplateMtia->Flag = true;
        }
        else
        {
            delete (localMatching[k]);
        }
    }
}

Debugging my code I realized that after the delete and clear of the vector matchingMtiae, the objects created were still allocated in memory and I can not understand the reason why this is happening since the pointer is not being lost but keeping it inside the vector.

I would like to deallocate the created objects from memory and completely clean the vector from pointers. Both are my aims.

Thanks in advance.


Solution

  • You can "submit" a non-binding request to the C++ library std::vector to release its allocated memory by calling shrink_to_fit after clear or resize.

    Note this is nonbinding which practically means every sane implementation actually releases memory but you cannot portably rely on this assumption strictly speaking.

    I would also strongly suggest replacing the raw pointers in your vector with std::unique_ptr (or even just the objects themselves, if there is no concern of inheritance/slicing). It will ease the visual load of your function and prevent memory leaks in the future.