简体   繁体   中英

Destructor of inherited class is not called [C++]

The desired program should run a simulation of a system by reading the different parameters in a configuration file and making an output file where are displayed the values of the position, speed, etc. For this purpose, the class Exercice4 and its derived class Question2 (inheritance) have been designed but it seems that the destructor of the inherited class Execice4 is not called during the destruction of the Question2 instance.

Here's a look at the declaration of the Exercice4 class:

typedef std::valarray<double> Vecteur;  
//------------------------------------------------------------------------------
class Exercice4{
  public :
    //Constructor
    Exercice4(int argc, char** argv);
    //Destructor
    virtual ~ Exercice4(){std::cout<<"Inside ~Exercice4"<<std::endl;outputFile->close();};
    
    void run();

  protected : //For inheritance
  
    std::string inputPath;
    unsigned int nsteps;
    std::unique_ptr<ConfigFile> configFile;
    double t, dt, tFin, dt_echantillonage;
    bool adaptatif;
    double epsilon; //=1 si adaptatif
    int sampling;
    int last;
    std::unique_ptr<std::ofstream> outputFile;

    virtual void printOut(bool)=0;
    virtual Vecteur f(Vecteur)const=0;
    virtual void step(bool)=0;
};

Here's a look at the declaration of the Question2 class:

class Question2 : public Exercice4 {
  public :
    Question2(int,char**);
    virtual ~Question2(){std::cout<<"Inside ~Question2"<<std::endl;};
  private :
    Vecteur Y; 
    virtual void printOut(bool) override;
    virtual Vecteur f(Vecteur) const override;
    virtual Vecteur RK4(Vecteur,double) const;
    virtual void step(bool) final;
};

Here's the main:

int main(int argc, char** argv){
  std::cout << "Début initialisation" << std::endl;
  Question2 simulation1(argc,argv);
  std::cout <<"Fin initialisation" << std::endl;
  simulation1.run();
  std::cout << "Début désallocation" << std::endl;
  return 0;
}

Here's the output for the terminal:

Début initialisation
Inside Exercice4
    nsteps=1000
    tFin=60
    adaptatif=1
    epsilon=1e-05
    sampling=1
    output=output.out
Outside Exercice4
    r_a=3.2e+08
    alpha=0.2024
    v=1100
Fin initialisation
Inside run
start run (while)
0
end run (while)
end run
Début désallocation
Inside ~Question2
free(): invalid next size (fast)
Aborted (core dumped)

We can see that the instantiation is ok (the values of the parameters are displayed on the terminal) but that there is this problem where it seems that the destructor of Execice4 is not called because then the terminal should display: Inside ~Exercice4 .

I checked if the destructor is declared as virtual (which he is) but I have no other idea how the problem should be resolved.

Thanks for the help !

Destructors are only needed when there are some dynamically allocated memory elements. When I look at your code, I can not see any dynamically allocated memory element. Unique pointer has its own destructor. Basically, there seems no need to use a destructor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM