The destructor was not called when the program was terminated using the exit() function in the function inside the object. Is there a way to terminate the program while calling the extinction?

Asked 2 years ago, Updated 2 years ago, 31 views

Hello, I am a student who is learning programming at the university.

This is an error that occurred while working on the team project last time, but I'm asking you a question because I think the project is over but it could be a problem later.

First of all, the code I used for the project at the time was too long, so I wrote a code with similar and the same error.

#include<iostream>
#include<stdlib.h>

using namespace std;
class testclass{
private:
public:
private:
public:
    testclass()
    {
        cout<<"hello"<<endl;
    }
    ~testclass()
    {
        cout<<"byebye"<<endl;
    }
    void exitcall(int input)
    {
        if(input)
        {
            exit(0);
        }
    }
};

int main()
{
    testclass a;
    int input;
    cin>>input;
    a.exitcall(input);
    return 0;
} 

My coding habit made the code longer for no reason, but the program itself is very simple.

Create a testclass object named a in the main and receive a number from the user.

If the number is 0, exit the program through the return located in the main

If the number entered is not 0, the program ends with the exit() function within the exitcall() function of the testclass.

At this time, to see if the constructor and destructor are executed, print out "hello" and

Outputs "bye" when the destructor is executed.

And next is the execution screen.

As you can see, when you enter 0 (i.e., when the program ends through return in main), the decider is called and "bye" is printed on the screen, but

When 1 was entered (i.e., the program terminated via exit() inside the object), the destructor was not called and the program terminated.

When I experimented with another code, it seems that the destructor is not called when the program is terminated with the exit() function, so is there a way to call the destructor of the object inside the object and terminate the program?

Additionally, I compiled using g++ in the ubuntu 14.04.5 LTS environment.

And when I was working on the project, I was using the ROS library, so instead of exit(), I used the ros::shutdown function to shut down the program and solved the problem. However, I posted a question because I was curious about how to solve it without using ROS, not because ROS can be easily used in all environments.

c++

2022-09-21 16:33

1 Answers

First, the destructor is called when the object is cleaned up. When object a is cleaned up is when the function call ends and the stack is cleaned up, and when the object is created on the heap and deleted is called.

However, the exit function does not clean up the stack.

Because the stack is not cleaned up, it does not remove the a object created in the stack, and the destructor is not called.

Is there a way to call the destructor of the object inside the object and terminate the program?
=> Is it necessary to make it an extinction? Can't we just call a function and end it?


    void aMethod()
    {
        cout<<"byebye"<<endl;
    }

    void exitcall(int input)
    {
        if(input)
        {
            aMethod();
            exit(0);
        }
    }

Try designing the program again.


2022-09-21 16:33

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.