-1

Why are the destructors not called?

Even though I've written destructors for class A and B, it's not being called.


#include<iostream.h> #include<conio.h> class A { public : A() { cout<<"\nIn A const."; } ~A() { cout<<"\nIn A dest."; } }; class B : public A { public : B() { cout<<"\nIn B const."; } ~B() { cout<<"\nIn B dest."; } }; int main() { A a; B b; getch(); return 0; } 

Why isn't constructor getting called?

4
  • Cannot reproduce. Why do you think they are not being called? Commented Sep 15, 2013 at 6:57
  • Because your compiler is totally broken? Or you are running an executable that doesn't correspond to this code? Could you post some real code that reproduces the problem? Commented Sep 15, 2013 at 6:58
  • 3
    On an unrelated note, prefer to stick your new lines at the end of the lines (you could even use cout << "My Text" << endl;). This works better in terminals that don't automatically insert a new line after program execution. Back to the question: what compiler are you using and I assume you have included <iostream>? Commented Sep 15, 2013 at 7:01
  • What libraries provide <iostream.h> and <conio.h>? Can you make a minimal example in standard C++? Commented Sep 29 at 10:52

3 Answers 3

3

Destructors of objects allocated on the stack is executed as soon as you exit the block containing the objects.

Your code would show the destructor being called before getch() if modified slightly

int main() { { A a; B b; } getch(); return 0; } 

Here I've used an extra pair of braces to define an inner block. Objects defined inside this block will be destroyed when the block is exited (thus before waiting for the keyboard input).

In your code the destructor is called when the main body block is exited, and this means AFTER waiting for keyboard.

Sign up to request clarification or add additional context in comments.

3 Comments

I tried this just now and destructor is never called.
@James: the destructor is for sure called. May be you are not seeing the output because the terminal is line-buffered. Add newlines after each output and you will see that in this code the constructor and the destructors are both executed before waiting for the character in getch().
Actually, I figured out what was happening. First, I put file writes in the destructors when proved they weren't getting called. I did some more research and discovered that I should in fact be using standard pointers to pass into functions and maintain references to parent/child classes. Once I did that, the destructors started getting called.
2

There is no problem with g++ 4.6

 mahmood@la:~$ g++ -o test test.cpp mahmood@la:~$ ./test In A const. In A const. In B const. In B dest. In A dest. In A dest.mahmood@la:~$ 

Comments

-2
class A { public : A() { cout<<"\nIn A const."; } virtual ~A() { cout<<"\nIn A dest."; } }; 

The desconstructor should be virtual.

3 Comments

what difference would this make?
This may cause memory link.
This only matters when when assigning a B* to an l-value of type A*. In that case if trying to delete this value as an A* with a non-virtual destructor the destructor of A will be called, but not that of B, whereas if the destructor is virtual the destructor of B will be called (and after that also that of A).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.