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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:27:26+00:00 2026-06-05T19:27:26+00:00

I have a class that I want to contain multiple objects of something I

  • 0

I have a class that I want to contain multiple objects of something I created. Right now the code that works is:

process.h:

private:
  myObj *data;

process.cc:

data = new myObj[10];

I would like to pass a value to the constructor however, so I tried to convert it to a std::vector (after modifying constructor to take a value).

process.h:

private:
  std::vector<myObj> data;

process.cc:

for (int m=0; m<10; m++) data.push_back( myObj(1.2) );

When I try that it crashes upon execution with

*** glibc detected *** ... corrupted double-linked list: ... ***

And the backtrace in gdb shows an error in the destructor when I tried to free some memory for other arrays I allocated. A search didn’t show up anything that was obvious. I am using a few static member variables in myObj, could that be an issue?

  • 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-05T19:27:28+00:00Added an answer on June 5, 2026 at 7:27 pm

    You are experiencing a double deletion bug. Consider this simple example:

    struct Other {};
    
    struct MyObj {
        Other *p;
    
        MyObj () : p(new Other) {}
        ~MyObj () { delete p; }
    };
    
    std::vector<MyObj> data;
    
    data.push_back(MyObj());
    

    The temporary object that is pushed onto data is stored properly. However, since it is a temporary, it is destroyed right after the push. This means, the p member is deleted when the temporary is destroyed, so the vector’s version of the copy has a dangling pointer. When the vector object is destroyed, the pointer will be deleted again, resulting in a corrupted heap. The error message you received was from the glibc code complaining about the resulting bad state.

    To fix this problem, a proper copy constructor should be defined, to pass ownership of the objects from the temporary to the destination. The rule of three says we should define the assignment operator as well.

    struct MyObj {
        mutable Other *p;
    
        MyObj () : p(new Other) {}
        MyObj (const MyObj &o) : p(o.p) { o.p = 0; }
        ~MyObj () { delete p; }
        const MyObj & operator = (MyObj o) {
            using namespace std;
            swap(*this, o);
            return *this;
        }
    };
    

    The use of mutable is required to be able to modify the p member when the instance is const, and const was needed because temporaries are being passed in to the copy constructor. With this change, pushing items into the vector work fine.

    A better solution would be to define p to use a unique_ptr instead.

    struct MyObj {
        std::unique_ptr<Other> p;
    
        MyObj () : p(new Other) {}
    };
    

    No destructor is needed in this example, since the default destructor will destruct p, which will cause the Other instance to be deleted by unique_ptr.

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

Sidebar

Related Questions

I want to have a template class that looks something like what I have
I want to have multiple objects share a reference through a private field, such
I have a class that contains multiple user objects and as such has an
I have a class that I want to include in a GWT module. Unfortunately,
Suppose I have a Python class that I want to add an extra property
I have a class that inherits QMainWindow and I just want it to have
I have a fairly simple class that I want to save to SQL Server
I have a class that is mapped in fluent nhibernate but I want one
I have a class that I've written a TypeConverter for. I want to keep
I have a class that instantiates two classes which implement interfaces. I want one

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.