How to make destructor called

I need help with this. Destructor for global and static object are not called

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>

class ConstructorDestructor {
public:
	ConstructorDestructor(int);
	~ConstructorDestructor();
private:
	int id;
};

ConstructorDestructor::ConstructorDestructor(int number) {
	id = number;
	std::cout << "constructor" << id << std::endl;
}

ConstructorDestructor::~ConstructorDestructor() {
	std::cout << "desctructor" << id << std::endl;
}

ConstructorDestructor first(1);

int main() {
	ConstructorDestructor second(2);
	static ConstructorDestructor third(3);

	return 0;
}


For me in this code constructors are called for all 3 objects but destructor only for second
The destructor is called for all three, in the right order.

See: http://coliru.stacked-crooked.com/a/8e2efa61c7d93a98
https://rextester.com/XITCG33518
@JLBorges
Yes that how it should be, but for me on my local windows debuger in visual studio, destructor are not called and I don't know why.
Are you sure that they are not called? What if the debugger does not show events that occur after end of main()?
@keskiverto
I'm new to programming so I'm not sure, can you tell me or direct me to some thread where I can learn how to check it. I'm trying to find answer for second day now.
If I add new functions in code after main, even messages for constructors after that functions arent printed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>

class ConstructorDestructor {
public:
	ConstructorDestructor(int);
	~ConstructorDestructor();
private:
	int id;
};

ConstructorDestructor::ConstructorDestructor(int number) {
	id = number;
	std::cout << "constructor" << id << std::endl;
}

ConstructorDestructor::~ConstructorDestructor() {
	std::cout << "desctructor" << id << std::endl;
}

void something(void);
ConstructorDestructor first(1);

int main() {
	something();
	ConstructorDestructor second(2);
	static ConstructorDestructor third(3);

	return 0;
}

void something(void) {
	ConstructorDestructor fourth(4);
}


this code for me print:
constructor1
constructor4

Edit:
If I define function 'something' before main than it call constructor for first, constructor and destructor for fourth, constructors for second and third and destructor for second.
Destructors for first and third are not printed.
Last edited on
This is what I get (for the second program) with the Visual Studio Debugger

constructor1
constructor4
desctructor4
constructor2
constructor3
desctructor2
desctructor3
desctructor1

C:\Users\xxx\source\repos\test\Debug\ConsoleApplication1.exe (process 11648) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->
Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .
To verify, you can place a break point in the destructor (on line 17) and then run the program under the debugger.
(Do not issue a 'Stop Debugging' command; 'Continue' till the program exits.)
Hello d34dc0d3,

This is the output from your 1st code:

constructor 1
constructor 2
constructor 3

desctructor 2
desctructor 3
desctructor 1


I suspect that your console window is closing B4 you can read the last 3 lines.

Try this:

  From the Menu tabs choose "Tools" then "Options".
  In the left side of the pop up window choose "Debugger" then "General".
  In the right window the last line should say "Automatically close the console when debugging stops".
  If the box is checked, uncheck it.
    Do not forget to undo the change if you want the console window to close.


This is for VS 2017, but I believe that VS 2019 is the same.

Andy
It finally work.
For some reason I thinked that when I put breakpoint on the end of file I need only once to start debuging but in these case it stop after first two constructor are created and than I must press continue and it print the rest.
Topic archived. No new replies allowed.