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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T17:02:16+00:00 2026-05-14T17:02:16+00:00

I am encountering the following bug. I have a class Foo . Instances of

  • 0

I am encountering the following bug.

  • I have a class Foo . Instances of this class are stored in a std::vector vec of class B.
  • in class Foo, I am creating an instance of class A by allocating memory using new and deleting that object in ~Foo().

the code compiles, but I get a crash at the runtime. If I disable delete my_a from desstructor of class Foo. The code runs fine (but there is going to be a memory leak).

Could someone please explain what is going wrong here and suggest a fix?

thank you!

class A{
      public:
          A(int val);
          ~A(){};
          int val_a;

};

A::A(int val){
       val_a = val;
       };

class Foo {      
      public:
             Foo();
             ~Foo();
             void createA();
             A* my_a;
};

Foo::Foo(){
    createA();
};

void Foo::createA(){
    my_a = new A(20);
};

Foo::~Foo(){
    delete my_a;

};



class B {
      public:
             vector<Foo> vec;             
             void createFoo();            
             B(){};
             ~B(){};
};


void B::createFoo(){
    vec.push_back(Foo());
};


int main(){
    B b;

    int i  =0;
    for (i = 0; i < 5; i ++){
        std::cout<<"\n creating Foo";
        b.createFoo();   
        std::cout<<"\n Foo created";
        }
    std::cout<<"\nDone with Foo creation";

    std::cout << "\nPress RETURN to continue...";
    std::cin.get();

    return 0;
}
  • 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-14T17:02:17+00:00Added an answer on May 14, 2026 at 5:02 pm

    You need to implement a copy constructor and an assignment operator for Foo. Whenever you find you need a destructor, you alnmost certainly need these two as well. They are used in many places, specifically for putting objects into Standard Library containers.

    The copy constructor should look like this:

    Foo :: Foo( const Foo & f ) : my_a( new A( * f.my_a ) ) {
    }
    

    and the assignment operator:

    Foo & Foo :: operator=( const Foo & f ) {
        delete my_a;
        my_a = new A( * f.my_a );
        return * this;
    }
    

    Or better still, don’t create the A instance in the Foo class dynamically:

    class Foo {      
          public:
                 Foo();
                 ~Foo();
                 void createA();
                 A my_a;
    };
    
    Foo::Foo() : my_a( 20 ) {
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following methods that are encountering some sort of failure with my
I'm encountering a consistency issue with an E_DEPRECATED message. I have the following snippet
I'm encountering following problem with the twitter bootstrap framework and jQuery: I have the
I'm encountering the following problem - I have simple GWT overlay types, and I'm
When attempting to set this up, I am encountering the following error: Traceback (most
I am encountering this following CXF exception: warning: Interceptor for {http://example.com/wsdl/esc/2011-12-12/}AmazonEC2#{http://example.com/wsdl/esc/2011-12-12/}NewDescribeImages has thrown exception,
I am encountering a strange bug in my code. I have a rails application
I am encountering the following warning /path/QtSDK/Desktop/Qt/474/gcc/include/QtCore/qglobal.h:320:6: warning: #warning This version of Mac OS
The following query that I have is encountering a divide by 0 error: select
I am encountering the following error in my app. upon starting it: [3/26/12 13:05:26:109

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.