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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:44:13+00:00 2026-05-25T00:44:13+00:00

I’m getting a segmentation fault which I believe is caused by the copy constructor.

  • 0

I’m getting a segmentation fault which I believe is caused by the copy constructor. However, I can’t find an example like this one anywhere online. I’ve read about shallow copy and deep copy but I’m not sure which category this copy would fall under. Anyone know?

MyObject::MyObject{
    lots of things including const and structs, but no pointers
}
MyObject::MyObject( const MyObject& oCopy){
    *this = oCopy;//is this deep or shallow?
}
const MyObject& MyObject::operator=(const MyObject& oRhs){
    if( this != oRhs ){
        members = oRhs.members;
        .....//there is a lot of members
    }
    return *this;
}
MyObject::~MyObject(){
    //there is nothing here
}

Code:

const MyObject * mpoOriginal;//this gets initialized in the constructor

int Main(){
    mpoOriginal = new MyObject();
    return DoSomething();
}

bool DoSomething(){
    MyObject *poCopied = new MyObject(*mpoOriginal);//the copy
    //lots of stuff going on
    delete poCopied;//this causes the crash - can't step into using GDB
    return true;
}

EDIT: Added operator= and constructor

SOLVED: Barking up the wrong tree, it ended up being a function calling delete twice on the same object

  • 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-25T00:44:14+00:00Added an answer on May 25, 2026 at 12:44 am

    Wow….

    MyObject::MyObject( const MyObject& oCopy)
    {
        *this = oCopy;//is this deep or shallow?
    }
    

    It is neither. It is a call to the assignment operator.
    Since you have not finished the construction of the object this is probably ill-advised (though perfectly valid). It is more traditional to define the assignment operator in terms of the copy constructor though (see copy and swap idium).

    const MyObject& MyObject::operator=(const MyObject& oRhs)
    {
        if( this != oRhs ){
            members = oRhs.members;
            .....//there is a lot of members
        }
        return *this;
    }
    

    Basically fine, though normally the result of assignment is not cont.
    But if you do it this way you need to divide up your processing a bit to make it exception safe. It should look more like this:

    const MyObject& MyObject::operator=(const MyObject& oRhs)
    {
        if( this == oRhs )
        {
            return *this;
        }
        // Stage 1:
        // Copy all members of oRhs that can throw during copy into temporaries.
        // That way if they do throw you have not destroyed this obbject.
    
        // Stage 2:
        // Copy anything that can **not** throw from oRhs into this object
        // Use swap on the temporaries to copy them into the object in an exception sage mannor.
    
        // Stage 3:
        // Free any resources.
        return *this;
    }
    

    Of course there is a simpler way of doing this using copy and swap idum:

    MyObject& MyObject::operator=(MyObject oRhs)  // use pass by value to get copy
    {
        this.swap(oRhs);
        return *this;
    }
    
    void MyObject::swap(MyObject& oRhs)  throws()
    {
        // Call swap on each member.
        return *this;
    }
    

    If there is nothing to do in the destructor don’t declare it (unless it needs to be virtual).

    MyObject::~MyObject(){
        //there is nothing here
    }
    

    Here you are declaring a pointer (not an object) so the constructor is not called (as pointers don;t have constructors).

    const MyObject * mpoOriginal;//this gets initialized in the constructor
    

    Here you are calling new to create the object.
    Are you sure you want to do this? A dynamically allocated object must be destroyed; ostensibly via delete, but more usually in C++ you wrap pointers inside a smart pointer to make sure the owner correctly and automatically destroys the object.

    int main()
    { //^^^^   Note main() has a lower case m
    
        mpoOriginal = new MyObject();
        return DoSomething();
    }
    

    But since you probably don’t want a dynamic object. What you want is automatic object that is destroyed when it goes out of scope. Also you probably should not be using a global variable (pass it as a parameter otherwise your code is working using the side affects that are associated with global state).

    int main()
    {
         const MyObject  mpoOriginal;
         return DoSomething(mpoOriginal);
    }
    

    You do not need to call new to make a copy just create an object (passing the object you want to copy).

    bool DoSomething(MyObject const& data)
    {
        MyObject   poCopied (data);     //the copy
    
        //lots of stuff going on
        // No need to delete.
        // delete poCopied;//this causes the crash - can't step into using GDB
        // When it goes out of scope it is auto destroyed (as it is automatic).
    
        return true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I have some data like this: 1 2 3 4 5 9 2 6
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small

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.