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 8961645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:56:31+00:00 2026-06-15T15:56:31+00:00

I am having trouble understanding how the C++ std::vector container would get freed in

  • 0

I am having trouble understanding how the C++ std::vector container would get freed in the following scenario

void DoSomething()
{
   cv::Point ** curves;
   int * curve_sizes;
   num_curves = m_curves.size(); //member variable holdings curves
   curves = new cv::Point*[num_curves];
   curve_size = new int[num_curves];

   std::vector<cv::Point> cur_points;
   for(int i = 0; i < num_curves; ++i)
   {
     cur_points = CreatePolyPoints(m_curves[i]);
     curves[i] = &cur_points[0];
     curve_sizes = cur_points.size();
   }

   cv::fillPoly(m_roi, curves, curve_sizes, num_curves, ... );

   //Clear the dynamic data
   // Do i do aything here?   
   delete [] curves;
   delete [] curve_sizes;
}

std::vector<cv::Point> CreatePolyPoints(Curve curve)
{
   std::vector<cv::Point> points;

   //Do work here here
   while(something)
   {
     cv::Point cur_point;
     points.push_back(cur_point);
   }

   return points;
}

Thanks in advance. If anyone is interested in my goal: Generate an ROI given polygons defined by “n” curves.

  • 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-06-15T15:56:33+00:00Added an answer on June 15, 2026 at 3:56 pm

    It is not dynamically allocated so it will be destroyed as soon as it goes out of scope. In your case, your variable will go out of scope after the DoSomething() function exits. Your local variable cur_points gets the return value of CreatePolyPoints() assigned to it within your loop. When this happens, the old contents of cur_points will no longer exist – you do not need to worry about this, as the instances of Curve objects within that vector will get destroyed each time the vector gets reassigned. Similarly when cur_points goes out of scope.

    EDIT: You do seem to have a problem here

    std::vector<cv::Point> cur_points;
    for(int i = 0; i < num_curves; ++i)
    {
       cur_points = CreatePolyPoints(m_curves[i]);
       curves[i] = &cur_points[0];    // <-- problem
       curve_sizes = cur_points.size();
    }
    

    You assign the address of the vector element 0 to your ith element of curves at each iteration – The problem here is that that will be the address of an object that no longer exists. This is because the contents of the vector will no longer exist once cur_points gets reassigned next time in the loop iteration, therefore the pointers you are storing do not point to valid objects – they point to objects that have been destroyed already. When your loop finished, the vector will contain the contents returned in the last call to CreatePolyPoints. If you want to make sure that all the cv::Point objects still exist at the point you call cv::fillPoly you should consider passing the vector by reference to the CreatePolyPoints() function rather than return a new vector by value each time. This will ensure that ALL items you add via CreatePolyPoints will still exist once your loop finishes

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

Sidebar

Related Questions

I'm having trouble understanding how to get the following working in interface builder. I've
I'm having trouble understanding the following bit of code that I was hoping would
I'm having trouble understanding why I get an error in the following code. I'm
Having trouble understanding. With the following css : .bloc .field:nth-last-child(2){ ...some values... } and
Im having trouble understanding class relationships after being asked to research it further, would
I'm having trouble understanding why I would use a context.xml file to declare a
Im having trouble understanding why the following code displays The First Key = (null)
I'm having trouble understanding why a click event bound to the document would be
I'm having a lot of trouble understanding pointers, and I've hit a point where
I'm having trouble getting MPI_Gatherv to work with a std::vector. I have written a

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.