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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:50:48+00:00 2026-06-17T14:50:48+00:00

I am trying to store objects that have pointers members in a std::vector. As

  • 0

I am trying to store objects that have pointers members in a std::vector. As far as I understand, when push_back is called, a temporary copy of the passed object is made and sent to the vector internal memory, and then it gets destroyed. Therefore, i wrote the copy constructor as shown below:

class MeltPoint
{
public:
    MeltPoint();
    MeltPoint(b2Vec2* point);
    MeltPoint(b2Vec2* point, Segment* segment, bool intersection);
    MeltPoint(MeltPoint const& copy);
    MeltPoint& operator= (const MeltPoint& m);
    ~MeltPoint();
private:
    b2Vec2* point;
    Segment* segment;
    bool intersection;
};

MeltPoint::MeltPoint()
{
    CCLog("MeltPoint DEFAULT CONSTRUCTOR");
}

MeltPoint::MeltPoint(b2Vec2* point)
{
    CCLog("MeltPoint CONSTRUCTOR");
    this->point = new b2Vec2();
    *(this->point) = *point;
    this->segment = new Segment();
    this->intersection = false;
}

MeltPoint::MeltPoint(b2Vec2* point, Segment* segment, bool intersection)
{
    this->point = point;
    this->segment = segment;
    this->intersection = intersection;
}

MeltPoint::MeltPoint(MeltPoint const& copy)
{
    CCLog("MeltPoint COPY");
    point = new b2Vec2();
    *point = *copy.point;

    segment = new Segment();
    *segment= *copy.segment;
}

MeltPoint& MeltPoint::operator= (const MeltPoint& m)
{
CCLog("MeltPoint ASSIGNMENT");
    *point = *m.point;
    *segment = *m.segment;
    return *this;
}

MeltPoint::~MeltPoint()
{
    CCLog("MeltPoint DESTRUCTOR");
    delete this->point;
    delete this->segment;
}

b2Vec2 (Box2D framework) is a struct that simply holds 2D coordinates

Segment is a custom class:

class Segment
{
public:
    Segment();
    Segment(b2Vec2* firstPoint, b2Vec2* secondPoint);
    ~Segment();

private:
    b2Vec2* firstPoint;
    b2Vec2* secondPoint;
};

Segment::Segment()
{
    CCLog("Segment DEFAULT CONSTRUCTOR");
    this->firstPoint = new b2Vec2(0, 0);
    this->secondPoint = new b2Vec2(0, 0);
}

Segment::Segment(b2Vec2* firstPoint, b2Vec2* secondPoint)
{
    CCLog("Segment CONSTRUCTOR");
    this->firstPoint = firstPoint;
    this->secondPoint = secondPoint;
}

Segment::~Segment()
{
    CCLog("Segment DESTRUCTOR");
    delete firstPoint;
    delete secondPoint;
}

In some function I am populating the vector:

void someFunction()
{
    vector<MeltPoint> randomVertices;
    randomVertices.push_back(MeltPoint(new b2Vec2(190, 170))); //10
    randomVertices.push_back(MeltPoint(new b2Vec2(70, 110))); //9
}

And the final output:

MeltPoint CONSTRUCTOR
Segment DEFAULT CONSTRUCTOR
MeltPoint COPY
Segment DEFAULT CONSTRUCTOR
MeltPoint DESTRUCTOR
Segment DESTRUCTOR
MeltPoint CONSTRUCTOR
Segment DEFAULT CONSTRUCTOR
MeltPoint COPY
Segment DEFAULT CONSTRUCTOR
MeltPoint COPY
Segment DEFAULT CONSTRUCTOR
MeltPoint DESTRUCTOR
Segment DESTRUCTOR
MeltPoint DESTRUCTOR
Segment DESTRUCTOR
test(1074,0xac7d9a28) malloc: *** error for object 0x844fd90: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
test(1074,0xac7d9a28) malloc: *** error for object 0x844fda0: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug

The errors are raised in the Segment destructor, but I allocated the two pointer members with a new in the constructor. Can you help me please?

  • 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-17T14:50:49+00:00Added an answer on June 17, 2026 at 2:50 pm

    Segment violates the rule of three. It lacks user-defined copy constructor and copy assignment operator. Whenever you make a copy of one, you end up with a double deletion.

    One fix could be following the rule of three and writing the copy constructor and copy assigment operator. But I won’t recommend that. I will recommend following the rule of zero instead. There is no need for custom destructors or custom copy constructors anywhere. Just give up on the idea of using dynamic memory allocation for naught.

    class MeltPoint
    {
    public:
        MeltPoint();
        MeltPoint(b2Vec2 const& point);
        MeltPoint(b2Vec2 const& point, Segment const& segment, bool intersection);
    
    private:
        b2Vec2 point;
        Segment segment;
        bool intersection;
    };
    
    MeltPoint::MeltPoint(b2Vec2 const& point)
    : point(point), segment(), intersection(false) {}
    
    MeltPoint::MeltPoint(b2Vec2 const& point, Segment const& segment, bool intersection)
    : point(point), segment(segment), intersection(intersection) {}
    
    class Segment
    {
    public:
        Segment();
        Segment(b2Vec2 const& firstPoint, b2Vec2 const& secondPoint);
    
    private:
        b2Vec2 firstPoint;
        b2Vec2 secondPoint;
    };
    
    Segment::Segment()
    : firstPoint(0, 0), secondPoint(secondPoint) {}
    
    Segment(b2Vec2 const& firstPoint, b2Vec2 const& secondPoint)
    : firstPoint(firstPoint), secondPoint(secondPoint) {}
    
    void someFunction()
    {
        vector<MeltPoint> randomVertices;
        randomVertices.push_back(MeltPoint(b2Vec2(190, 170))); //10
        randomVertices.push_back(MeltPoint(b2Vec2(70, 110))); //9
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that contains two arraylists which I'm trying to store objects
I have a vector that stores pointers to many objects instantiated dynamically, and I'm
I understand pointers are used to point to objects so you would have to
I am trying to make a configuration manager class, that can store arbitrary objects
I'm trying to use HashSet to store objects of a class that I created,
I'm trying to create an NSDictionary that stores objects with keys based on IDs.
I'm trying to return a double from another object then store that into a
I am very new to processing.org and Java. I am trying to store objects
Im trying to store a list,collection of data objects in Hbase. For example ,a
I'm trying to store an image into an SQL database without using temporary files.

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.