Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4237226
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:44:44+00:00 2026-05-21T02:44:44+00:00

I’m currenty writing small application for image processing. However I have a big problem

  • 0

I’m currenty writing small application for image processing. However I have a big problem with memory usage of my program. I’m fairy new in c++, previously I mainly was programing in c#.

The function which do almost all the work looks like that

while(!prototypeVector[i]->GetIsConvergaed()) 
{
    if(previousPrototype!=NULL) delete previousPrototype;
    previousPrototype = prototypeVector[i]->CopyPrototype();

    if(&matchingPointVector!=NULL) matchingPointVector.clear();
    matchingPointVector = prototypeVector[i]->CalculateMatchingPointsAll(imageDataVector); 
    distanseMatrix = CalculateDistanceAll(i);

    membershipMatrix = UpdateMembershipAll(i);

    if(translation)
    {
        tmatrix = UpdateTranslationMatrix(i);
        if(directUpdate) prototypeVector[i]->SetTranslationMatrix( tmatrix);

        //prototypeVector[i]->GetTranslationMatrix().DisplayMatrix();
        tmatrix.DisplayMatrix();
    }
    if(scaling)
    {
        smatrix = UpdateScalingMatrix(i);
        if(directUpdate) prototypeVector[i]->SetScalingMatrix(smatrix);
        smatrix.DisplayMatrix();
    }
    if(rotation)
    { 
        angle =  UpdateAngleCoefficient(i);
        cout<<endl;
        Convert::RadiansToDegrees(angle)<<endl;
        if(directUpdate)prototypeVector[i]->UpdateRotationMatrix(angle);
    }

    prototypeVector[i]->TransformTemplateOne(prototypeVector[i]->GetRotationMatrix(), prototypeVector[i]->GetScalingMatrix()  , prototypeVector[i]->GetTranslationMatrix());
}

I noticed that if in function written above is called another function

CalculateMatchingPointsAll or CalculateDistanceAll or UpdateScalingMatrix the memory usage rises dramatically about (300kB after execution each of functions mentioned above). So I suppose the problem is in these functions. They look like that

vector<Point*> TemplateClusterPoint::CalculateMatchingPointsAll( vector<Point*> imageDataVector)
{
    vector<Point*> matchinPointVector = vector<Point*>(imageDataVector.size(),new Point(0,0));
    double minValue = DOUBLE_MAX_VALUE;
    double currentDistance = 0;
    for (int i=0;i<imageDataVector.size();i++ )
    {
        //matchinPointVector[i] = this->CalculateMatchingPoint(/*prototypePointVector,*/imageDataVector[i]);
        for (int j=0;j<prototypePointVector.size();j++)
        {
            if( (currentDistance = CalculateDistance(imageDataVector[i],prototypePointVector[j]) ) <= minValue )
            {

                minValue = currentDistance;
                matchinPointVector[i] = prototypePointVector[j];
            }
        }
        minValue =   currentDistance = DOUBLE_MAX_VALUE;
    }
    return matchinPointVector;
}


vector<vector<double>> AlgorithmPointBased::CalculateDistanceAll( int clusterIndex)
{
    //vector<Point*> pointVector = prototypeVector[clusterIndex]->GetPrototypePointVector();
    Point* firstPoint = NULL;
    Point* secondPoint = NULL;
    for(int i=0;i<imageDataVector.size();i++ )
    {
        firstPoint = imageDataVector[i];
        secondPoint = matchingPointVector[i];

        distanseMatrix[clusterIndex][i] =  pow( (firstPoint->GetX() - secondPoint->GetX() ), 2    ) + pow( (firstPoint->GetY() - secondPoint->GetY() ), 2);   //(difference*difference)[0][0]; //funkcja dystansu = d^2 = (Xi - Pji)^2
                                                                                    // gdzie Xi punkt z obrazu, Pij matching point w danym klastrze
    }
    return distanseMatrix;
}

Matrix<double> AlgorithmPointBased::UpdateScalingMatrix( int clusterIndex )
{

    double currentPower = 0;
    vector<Point*> prototypePointVecotr = prototypeVector[clusterIndex]->GetPrototypePointVector();
    vector<Point*> templatePointVector = templateCluster->GetTemplatePointVector();
    Point outcomePoint;
    Matrix<double> numerator =  Matrix<double>(1,1,0);
    double denominator=0;
    for (int i=0;i< imageDataVector.size();i++)
    {
        Point templatePoint =  *matchingPointVector[i]; 
         currentPower = pow(membershipMatrix[clusterIndex][i],m);
        numerator += /  ((*imageDataVector[i] - prototypeVector[clusterIndex]->GetTranslationMatrix()).Transpose()* (prototypeVector[clusterIndex]->GetRotationMatrix() * templatePoint )* currentPower);
        denominator += (currentPower* (pow(templatePoint.GetX(),2) +  pow(templatePoint.GetY(),2)));   
    }
     numerator /= denominator;
    return numerator;
}

As You can see almost all the work these function do is calculating new points or closest point or transormating an image. Is there any way to somehow release at least some memory after these functionos were executed.
I suppose that the most memory consuming operations are multiplication of matrices or operations on points. I have overloaded operator + * and / which of course return new objects.

EDITED
Overloaded operators look like that

Point Point::operator*( double varValue )
{
    return *(new Point(this->x * varValue,this->y * varValue));
}

Point Point::operator-( Point& secondPoint )
{
    return *(new Point(this->x - secondPoint.GetX(),this->y - secondPoint.GetY()));
}

Point Point::operator*( double varValue )
{
    return *(new Point(this->x * varValue,this->y * varValue));
}

template<typename T>
Matrix<T> Matrix<T>::operator + (double value)
{
    Matrix<T>* addedMatrix = new Matrix<T>(this->rows,this->columns);
    for (int i=0;i<this->rows;i++)
    {
        for (int j=0;j<this->columns;j++)
        {
            (*addedMatrix)[i][j] = (*this)[i][j]+ value;
        }
    }
    return *addedMatrix;
}

Point Point::operator/( double varValue )
{
    return *(new Point(this->x / varValue,this->y / varValue));
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T02:44:45+00:00Added an answer on May 21, 2026 at 2:44 am

    I’m fairy new in C++, previously I mainly was programing in C#.

    Unlike C#, C++ has no garbage collection. Any time that you use new (e.g., new Point(0,0)), you are responsible for using delete to destroy that object.

    Ideally, you should avoid dynamically allocating objects explicitly and eschew new and delete altogether. You should prefer to create objects with automatic storage duration (on the stack) and work with copies of them (by passing them by value or reference, returning them by value, and storing copies of them in containers).

    Among other things, you should almost certainly be using a std::vector<Point> instead of a std::vector<Point*>.


    return *(new Point(this->x * varValue,this->y * varValue)); 
    

    All of your functions that have code like this are wrong: you dynamically allocate an object, return a copy of that object, and lose all references to the original. You are left without any way to destroy the dynamically allocated object. You don’t need to dynamically allocate anything here. The following would suffice:

    return Point(x * varValue, y * varValue);
    

    if(&matchingPointVector!=NULL)
    

    This can never be false in a correct program: & takes the address of an object and no object can have the address NULL. The only way this could happen is if you had already dereferenced a null pointer somewhere in your program, in which case you’re already in a heap of trouble.


    Make sure that you have a good introductory C++ book. C++ and C# are very different programming languages.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.