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

  • Home
  • SEARCH
  • 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 6386491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:02:00+00:00 2026-05-25T03:02:00+00:00

I made a function for an object called copy() that should just return an

  • 0

I made a function for an object called copy() that should just return an instance of the object with all the same values –

Grid Grid::copy() {

Grid result;

result.setFilename(f_name);
result.setNumOfRows(num_rows);
result.setNumOfCols(num_cols);
result.setMap(map);


return result;
}

My destructor looks like this –

Grid::~Grid() {
for(int r=0;r<num_rows;r++)
    delete [] map[r];
}  

Now whenever my code is running and the copy function gets called, I get an error

*** glibc detected *** ./go: double free or corruption (!prev): 0x0982c6a8 ***

with a lot of other information (big wall of text) after that. That just means the memory is being deleted twice correct? If so, how can this be? Why does the destructor get called twice?

The code where it gets called looks like this –

for(;;) {
    Grid g;

    if(which_display == 1) {

       .....
       .....
        g = myServer->getAgent()->getGrid()->copy(); //HERE


    }
    //print
    std::cout<<g.toString();
}

I feel like I’m missing something obvious. Can someone point out to me how the destructor is being called twice?

  • 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-25T03:02:00+00:00Added an answer on May 25, 2026 at 3:02 am

    You don’t actually want a copy method at all. You simply want a copy constructor and assignment operator. I’m guessing your line originally looked like this:

    g = myServer->getAgent()->getGrid();
    

    And since that didn’t work, you added the copy method. But the way it is now, fixing your copy method alone will not solve the problem, as you need the copy constructor and assignment operator too, or your hard work in fixing the copy method could be destroyed.

    First, a quick explanation as to what is happening, and why the program fails:

    1. You call copy
    2. This enters your copy method, which creates a Grid on the stack.
    3. It sets the members of the Grid, but we suspect it does a shallow copy.
    4. The copy method returns, which invokes the copy constructor of Grid. *
    5. The default copy constructor does a shallow copy.
    6. The stack-based Grid‘s destructor fires, deleting the contents of map.
    7. The copy method has now returned, providing a temporary Grid, but one that points to deleted memory.
    8. Now the temporary Grid object is assigned into g. This invokes the assignment operator of Grid. *
    9. The default assignment operator does a shallow copy, just like the default copy constructor did.
    10. At the end of the line, the temporary object is destroyed, where it tries to delete the contents of map — which were already deleted. boom.
    11. When g goes out of scope, its destructor will try to delete the contents of map yet again.

    As you can see, there are 3 places where a shallow copy occurs — all must be fixed or this will still fail.

    How to fix this

    1. Get rid of the copy method — it doesn’t provide any value anyway.
    2. Fix your setMap and setFilename to do a deep copy.
    3. Create an assignment operator. This should deep-copy the contents of the other Grid.
    4. Create a copy constructor, just like the assignment operator.

    Here’s what the assignment operator could look like (assuming all set methods do deep copy):

    Grid& operator= (const Grid& g) {
      setFilename(f_name);
      setNumOfRows(num_rows);
      setNumOfCols(num_cols);
      setMap(map);
    
      return *this;
    }
    

    There are techniques to write the copy constructor, and then make the assignment operator use the copy constructor. This is a good technique (less duplicated code), but I don’t have the link handy. When I find it, I’ll link it here.

    Lastly, I marked a few lines in my explanation(*). Compilers can do Return Value Optimization (RVO), and Named RVO. With these optimizations, it would not actually create the Grid object on the stack within copy, and then copy-construct for the return value — it would simply create the temporary object for the result of copy, and then the copy method would use that instead of its own internal stack-based Grid object. So with enough compiler optimizations, your code might make it past this point and crash later. Obviously that’s not helpful, so this is more of an fyi.

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

Sidebar

Related Questions

Anyone got a ready made function that will take an XML string and return
I've made some function that generates an email template. Code it generates is pure
Hi have made this function which is made to replicate an error that I
I have made a function to cound the weeks in a year, and that
Lets suppose I made a class called Person. var Person = function(fname){this.fname = fname;};
I'm using an entity object called User. This is the function I use to
I'm attempting to use the __rsub__ function in a class I've made called Fraction.
I have made webpage that uses Ajax to update some values without reloading the
I have a function, void Validate() , that contains all my validation logic for
I made a function which displays date on the webpage,, and i uploaded the

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.