S
someone
Hi
See this (stolen from the internet):
----------------------------------------
#include <iostream>
using namespace std;
class Base
{
public:
Base(){ cout<<"Constructing Base"<<endl;}
// this is a destructor:
virtual ~Base(){ cout<<"Destroying Base"<<endl;}
};
class Derive: public Base
{
public:
Derive(){ cout<<"Constructing Derive"<<endl;}
~Derive(){ cout<<"Destroying Derive"<<endl;}
};
int main()
{
Base *basePtr = new Derive();
// delete basePtr; // <<<======== !! ! ! ! ! NB !!
return 0;
}
----------------------------------------
See middle line of main():
delete basePtr; // <<<======== !! ! ! ! ! NB !!
If I enable (uncomment) middle line, I get:
Constructing Base
Constructing Derive
Destroying Derive
Destroying Base
If I disable (comment out) middle line, I get:
Constructing Base
Constructing Derive
========
This, I didn't quite expect, because I thought that at the end of the
program, all destructors where guaranteed to be called...
In my case, I have a longer program and I allocate memory with one of my
constructors, hence it is crucial to free the memory, but since the
virtual destructor is never called, I never free the memory...
In addition: In my full program (not shown here), I instantiate my new
object like:
int main(...)
{
...
Derive d1();
return 0; // or exit(1); if something went wrong somewhere
}
Is it required that I do, like in the example above:
int main(...)
{
...
Base *myPtr = new Derive();
delete myPtr;
return 0; // or exit(1); if something went wrong somewhere
}
???????
But what if my code does an exit(1) somewhere, which it could do at
least a dozens of times, in different parts of the program?
Please advice, thank you!
See this (stolen from the internet):
----------------------------------------
#include <iostream>
using namespace std;
class Base
{
public:
Base(){ cout<<"Constructing Base"<<endl;}
// this is a destructor:
virtual ~Base(){ cout<<"Destroying Base"<<endl;}
};
class Derive: public Base
{
public:
Derive(){ cout<<"Constructing Derive"<<endl;}
~Derive(){ cout<<"Destroying Derive"<<endl;}
};
int main()
{
Base *basePtr = new Derive();
// delete basePtr; // <<<======== !! ! ! ! ! NB !!
return 0;
}
----------------------------------------
See middle line of main():
delete basePtr; // <<<======== !! ! ! ! ! NB !!
If I enable (uncomment) middle line, I get:
Constructing Base
Constructing Derive
Destroying Derive
Destroying Base
If I disable (comment out) middle line, I get:
Constructing Base
Constructing Derive
========
This, I didn't quite expect, because I thought that at the end of the
program, all destructors where guaranteed to be called...
In my case, I have a longer program and I allocate memory with one of my
constructors, hence it is crucial to free the memory, but since the
virtual destructor is never called, I never free the memory...
In addition: In my full program (not shown here), I instantiate my new
object like:
int main(...)
{
...
Derive d1();
return 0; // or exit(1); if something went wrong somewhere
}
Is it required that I do, like in the example above:
int main(...)
{
...
Base *myPtr = new Derive();
delete myPtr;
return 0; // or exit(1); if something went wrong somewhere
}
???????
But what if my code does an exit(1) somewhere, which it could do at
least a dozens of times, in different parts of the program?
Please advice, thank you!