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 am encountering an exception cause by the CFINVOKEARGUMENT line of the following snippet:
Google chrome doesn't behave the same as other browsers when encountering this nugget: <?php
I'm amused by a strange error that I'm encountering in my program. I have
I've been encountering the following error when trying to build a session factory: PersistenceTests.Can_Map_Orders_To_Database
I'm encountering the following error at unpredictable times in a linux-based (arm) communications application:
I am encountering very infrequent yet annoying SQL deadlocks on a .NET 2.0 webapp
I'm encountering some peculiarities with LINQ to SQL. With a relatively simple query, I
Iv been encountering problems with disk space when deploying my app to a pocket
Hi i am encountering problems trying to post a WebRequest under Https. i received
I'm encountering problems with my .NET Framework 3.0 SP1 application. It is a C#

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.