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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:01:37+00:00 2026-05-22T21:01:37+00:00

I have a class: class A { public: A() { std::cout << Constructor called

  • 0

I have a class:

class A
{
public:
    A()
    {
        std::cout << "Constructor called" << std::endl;
    }

    ~A()
    {
        std::cout << "Destructor called" << std::endl;
    }

    A(const A& another)
    {
        std::cout << "Copy constructor called" << std::endl;
    }

    A& operator=(const A& another)
    {
        std::cout << "Assignment operator= called" << std::endl;
    }
};

In my very complicated project, I got the following output after I started my app:

Constructor called

but when I Ctrl+C to terminate my app:

Destructor called
Destructor called

And in the whole process, no copy constructor or assignment operator got called.

My class A has dynamic memory allocation, and I have to free it in the destructor, but the destructor is called twice somehow, which is very bad.

I don’t know what could cause this problem.

I googled and searched a lot. A lot of questions about “destructor called twice” is because of the the implicit calling of the copy constructor (assignment operator).

Thanks.

Peter

  • 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-22T21:01:38+00:00Added an answer on May 22, 2026 at 9:01 pm

    If you are somehow calling the destructor twice, maybe you have two objects that think they own it through a pointer.

    If you really do have two objects that think they own this one, consider using a reference counted pointer such as Boost::shared_ptr or tr1::shared_ptr to contain it. That way you don’t have to worry about who calls the destructor finally.

    Aside from the debugger, you could try Valgrind (memcheck) to find where your program deletes an already freed object. It probably won’t give more detail than the debugger in this case, but you ought to learn to use Valgrind (memcheck) sooner or later.

    Another paranoid strategy is to make sure to set any internal pointers to NULL after you delete them.

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

Sidebar

Related Questions

For example, suppose I have a class: class Foo { public: std::string& Name() {
#include<iostream> using namespace std; class A { public: int i; A() {cout<<A()<<endl;} ~A() {cout<<~A()<<endl;}
Let's say i have 2 classes: class Class1 { public: std::vector<CustomClass3*> mVec; public: Class1();
I have a class like this: class MyClass{ public: MyClass(int Mode); private: std::map <
I have a class that includes a std::list and wish to provide public begin()
I have class Fred { public: void inspect() const {}; void modify(){}; }; int
I have the following code #include <iostream> using namespace std; class Object { public:
I have this code #include <iostream> using namespace std; class Test{ public: int a;
assume the following class is given: class Base{ public: Base() {} Base( const Base&
Hi I have some issue regarding constructor and destructor. I have list class, which

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.