Topic: Is an object still intact on entry of its destructor?


Author: Bertwim <bwvb@xs4all.nl>
Date: Mon, 8 Feb 2010 11:38:32 CST
Raw View
Can I, for instance do something like this:


foo::~foo()
{
      _registry.unregister( *this );
}

Where the assumption is that the function unregister( foo& )
still receives a valid object, although it is being called from within
the objects' destructor.

Regards,
Bertwim

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Kaz Kylheku <kkylheku@gmail.com>
Date: Mon, 8 Feb 2010 13:41:56 CST
Raw View
On 2010-02-08, Bertwim <bwvb@xs4all.nl> wrote:
>
> Can I, for instance do something like this:
>
>
> foo::~foo()
> {
>       _registry.unregister( *this );
> }
>
> Where the assumption is that the function unregister( foo& )
> still receives a valid object, although it is being called from within
> the objects' destructor.

This assumption is false if the foo object is actually a
base:

 class bar : public foo { ... };

When the body of the ~foo destructor is running, the foo base part of
the object is still valid, but not the bar part. So the object
as a whole is not intact.

So your unregister function can't do anything which depends on the
derived parts still being there. If it uses virtual functions, they will
not go to any overrides that are more specific than foo.

If all that unregister does is look for the pointer in a map and delete
it, that's innocuous.

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "Johannes Schaub (litb)" <schaub-johannes@web.de>
Date: Tue, 9 Feb 2010 10:54:33 CST
Raw View
Bertwim wrote:

>
> Can I, for instance do something like this:
>
>
> foo::~foo()
> {
>       _registry.unregister( *this );
> }
>
> Where the assumption is that the function unregister( foo& )
> still receives a valid object, although it is being called from within
> the objects' destructor.
>

If the destructor has started execution, the lifetime of the object ends. If
the lifetime of an object ended, its properties aren't in effect anymore
(like, the type of an object is not known, see 3.8/3 in n3000) - except
during destruction or during construction. So in the constructor and
destructor, special rules apply that allow you to use "*this" like if it
were an object of type "foo". As another poster said, though, you are not
allowed to access the derived class object anymore tho.

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]