Topic: Explicit destructor call


Author: Ian Haggard <ian@shellus.com>
Date: 1997/01/06
Raw View
In article <STnyyEpfhGBH090yn@nada.kth.se>,
  d96-mst@nada.kth.se (Mikael Steldal) wrote:
> int main(void)
> {
>       Test test;
>
>       cout << "About to explicitly call destructor for test" << endl;
>       test.~Test();
>
>       cout << "Done in main()" << endl;
>
>       return 0;
> }
>
> I my compiler, the destructor is called twice. Is that correct? Why
> should it be like that?
This is correct, as other respondents have noted.  If what you wish to
accomplish is destroying the object before function exit (say, you have
a very
large automatic array or memory-hog data-structure that you're using for
only
part of the function) you can recode your main() like so:

int main(void)
{

// create scope for variable test
    {
        Test test;

        cout << "About to destroy test" << endl;
    }
// end scope of variable test (calls destructor for test, pops it off
the stack)

        cout << "Done in main()" << endl;

        return 0;
}

Ian Haggard || ian@shellus.com (work) || IanHaggard@juno.com (home)
Linux -- "Oh, no, Mr Bill!" || #define employer_opinion !my_opinion
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: stephen.clamage@Eng (Steve Clamage)
Date: 1997/01/03
Raw View
In article STnyyEpfhGBH090yn@nada.kth.se, d96-mst@nada.kth.se (Mikael St   ldal) writes:
>#include <iostream.h>
>
>class Test {
>public:
> Test() { cout << "Test default constructor" << endl; }
> ~Test() { cout << "Test destructor" << endl; }
>};
>
>int main(void)
>{
> Test test;
> cout << "About to explicitly call destructor for test" << endl;
> test.~Test();
> cout << "Done in main()" << endl;
> return 0;
>}
>
>I my compiler, the destructor is called twice. Is that correct? Why
>should it be like that?

The compiler is required to destroy auto variables at function exit.
If you also call the destructor explicitly, it gets called twice.
(The results of the second destruction are undefined.)

Why are you allowed to call a destructor explicitly? Because it is
sometimes necessary or desirable. In the above example, it is just
plain wrong.

You very seldom need or want to call a destructor explicitly.
Normally you do so only when you are providing your own memory
managment. But auto variables are called "auto" because they
are automatically managed for you.

Why doesn't the compiler detect the double destruction? Consider this:

 extern g(T&);  // in a precompiled 3rd party library

 void f()
 {
  T t;
  g(t); // does g destroy its argument?
 }

In principle the runtime system could keep track of whether an object
has been destroyed, but that would require noticeable extra overhead
for every non-trivial object in every program. An implementation that
checks thoroughly for errors might choose to do so, but it is not a
language requirement. Similarly, a compiler might choose to do a
flow analysis and discover that "test" was destroyed twice (but be
unable to determine whether "t" was destroyed twice), but that also
is not a language requirement.

---
Steve Clamage, stephen.clamage@eng.sun.com




[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: d96-mst@nada.kth.se (Mikael St ldal)
Date: 1997/01/03
Raw View
#include <iostream.h>

class Test
{
public:
 Test() { cout << "Test default constructor" << endl; }

 ~Test() { cout << "Test destructor" << endl; }
};

int main(void)
{
 Test test;

 cout << "About to explicitly call destructor for test" << endl;
 test.~Test();

 cout << "Done in main()" << endl;

 return 0;
}

I my compiler, the destructor is called twice. Is that correct? Why
should it be like that?


[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: comeau@panix.com (Greg Comeau)
Date: 1997/01/03
Raw View
In article <STnyyEpfhGBH090yn@nada.kth.se> d96-mst@nada.kth.se (Mikael St   ldal) writes:
>#include <iostream.h>
>
>class Test
>{
>public:
> Test() { cout << "Test default constructor" << endl; }
>
> ~Test() { cout << "Test destructor" << endl; }
>};
>
>int main(void)
>{
> Test test;
>
> cout << "About to explicitly call destructor for test" << endl;
> test.~Test();
>
> cout << "Done in main()" << endl;
>
> return 0;
>}
>
>I my compiler, the destructor is called twice. Is that correct? Why
>should it be like that?

test has an implicit 'auto' in its declaration.  That is to say,
it is:

    auto Test test;

auto means automatic.  The compiler will take care of it.
It will automatically call the ctor and automatically call the dtor.
But since you explicitly call the dtor, that, plus the automatic one
it two.  Unless you have extenuating circumstances, don't do that.

- Greg
--
       Comeau Computing, 91-34 120th Street, Richmond Hill, NY, 11418-3214
               Producers of Comeau C++ 4.0 front-end pre-release
****WEB: http://www.comeaucomputing.com / Voice:718-945-0009 / Fax:718-441-2310
 Here:comeau@comeaucomputing.com / BIX:comeau or comeau@bix.com / CIS:72331,3421


[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]