C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Virtual calls upon destruction

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Jeremy Jurksztowicz
Guest





PostPosted: Wed Nov 01, 2006 10:10 am    Post subject: Virtual calls upon destruction Reply with quote



Hi,

I have a class heirarchy which has a uninitialize() virtual member
function, which aught to be called upon destruction. I know that
putting it in the destructor will not work, so I came up with the
following solution. Aside from any aesthetic and maintenance problems
(I am aware of a few), is this technique portable?

class Base
{
public:
virtual ~Base ();
virtual void uninitialize ( ) = 0;
virtual void deleteMe (void * ptr, std::size_t sz)
{ ::operator delete(ptr, sz); }
// ...

static void operator delete (void * ptr, std::size_t sz)
{
if(ptr)
{
Base * bptr = reinterpret_cast<Base*>(ptr);
bptr->uninitialize();
bptr->deleteMe(ptr, sz);
}
}
};

So what the hell have I done? I am assuming that bptr will point to a
fully constructed object, and thus the correct functions will be
called. I am on a trip now, and don't have access to a compiler, so I
may be completely bonkers on this one, any comments are appreciated.

Regards,
Jeremy Jurksztowicz


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Roland Philippsen
Guest





PostPosted: Thu Nov 02, 2006 3:53 am    Post subject: Re: Virtual calls upon destruction Reply with quote



ralph wrote:
Quote:
Jeremy Jurksztowicz wrote:

I have a class heirarchy which has a uninitialize() virtual member
function, which aught to be called upon destruction. I know that
putting it in the destructor will not work, so I came up with the
following solution. [...]

Why shouldn't it work? Polymorphic calls don't work in constructors.
But they should work in destructors.

Nope, I've stumbled across this one several times. AFAIK, in the dtor
the type of *this is the class being destroyed, and the vtable is
adjusted accordingly before entering the dtor.

Cheers,
Rolo

--
Roland Philippsen
Autonomous Systems Lab
http://www.asl.ethz.ch/

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
James Kanze
Guest





PostPosted: Thu Nov 02, 2006 3:56 am    Post subject: Re: Virtual calls upon destruction Reply with quote



Bob Bell wrote:
Quote:
Jeremy Jurksztowicz wrote:

[snip]

So what the hell have I done? I am assuming that bptr will point to a
fully constructed object, and thus the correct functions will be
called.

Nope; by the time operator delete is called, all destructors will be
executed and bptr will point to uninitialized memory.

Instead, you want something like this:

class Base
{
public:
virtual ~Base();
void deleteMe()
{
this->uninitialize();
delete this;
}
virtual void uninitialize() = 0;
};

class Derived : public Base
{
public:
virtual void uninitialize();
};

void F()
{
Derived* d(new Derived());
// ...
d->deleteMe();
}

This has the disadvantage that there's no way in general to stop
someone from deleting such a pointer directly and circumventing the
uninitialize() member function, but some improvements can be made
(e.g., making the Base destructor protected).

Having gotten this far, I'm surprised that you didn't think of
the obvious: define a private operator delete() in Base. A
Derived can still define a public operator delete and override
this, but that sounds like an outside chance to me, and the
casual user can no longer accidentally call delete on the
object.

--
James Kanze (Gabi Software) email: james.kanze (AT) gmail (DOT) com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Bob Bell
Guest





PostPosted: Fri Nov 03, 2006 5:20 am    Post subject: Re: Virtual calls upon destruction Reply with quote

Jeremy Jurksztowicz wrote:
Quote:
Nope; by the time operator delete is called, all destructors will be
executed and bptr will point to uninitialized memory.

Hmm... I am curious as to how this occurs? Is this mandated by the
standard (seems likely)? Is there a hidden, behind the scenes operation
that calls T::~T()? In retrospect this seems obvious, given that an
untyped (void) pointer is passed to operator delete. I am imagining
operator delete gets implemented something like this (pardon my
ignorance):

void _impl_delete (_impl_delete_ptr_type * ptr, std::size_t sz)
{
_impl_call_destructor(ptr);

// Calls correct delete operator
_impl_call_delete(
reinterpret_cast<void*>(ptr), sz);
}

Now might such a function be called by a C++ implementation 'behind the
scenes', so to speak?

It sounds like you're confusing operator delete (the memory
deallocation function which can be overridden globally or at the class
level) with the delete operator (which calls the destructor of an
object, then calls operator delete to free the object's memory, and is
not overridable).

Assuming we have a pointer p that points to a T object, this code:

delete p;

gets compiled as if it were written:

p->~T();
T::operator delete(p);

(If T has no operator delete(), then the global operator delete is used
instead.)

You can't change the behavior of the delete operator. The only thing
you can do is try to force users to make extra calls before deleting.

class Base
{
public:
void deleteMe();
virtual void uninitialize() = 0;
private:
static void operator delete(void*); // Thanks to James Kanze for
reminding me of this...
};

void Base::deleteMe()
{
this->uninitialize();

delete this;
}

Now, the only way to delete a pointer to a Base is to used
Base::deleteMe(), which calls the uninitialize() member function and
then deletes the object.

(As James pointed out, a derived class could always make a public
operator delete which could then be used to delete a derived class
object, but I would classify that as a misuse of Base.)

Bob


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Jeremy Jurksztowicz
Guest





PostPosted: Fri Nov 03, 2006 10:10 am    Post subject: Re: Virtual calls upon destruction Reply with quote

Quote:
It sounds like you're confusing operator delete (the memory
deallocation function which can be overridden globally or at the class
level) with the delete operator (which calls the destructor of an
object, then calls operator delete to free the object's memory, and is
not overridable).

That was it, thanks a lot!

Just out of curiosity, would such a beast work?

class Base
{
typedef boost::function<void(void)> Uniniter;
Uniniter uninit_;
public:
Base(Uniniter const& u): _uninit(u) { }
virtual ~Base ( ) { uninit_(); }

virtual void uninitialize ( ) = 0;
};

class Derived : public Base
{
public:
Derived ( ): Base(boost::bind(&Derived::uninitialize, this)) { }

void uninitialize ( );
};

Now I wonder, would the bound 'this' parameter in Derived's consruction
be valid during ~Base()? By the time ~Base() is run I assume that
~Derived() has already run, but does this 'unwind' the vptr, or by some
other method make Derived::uninitialize() uncallable (or unsafe)? I am
aware that poiner to member functions are weird, and are supposed to
display polymorphic behavior, but in this case I am boost::binding
right to the function I want called. Let me say again, just curious.

Anyways, your suggestion of using a deleteMe() member function seems
much more sane to me, and since I already use shared_ptr for the
objects, painless to implement. Thanks again.

Regards,
Jeremy Jurksztowicz


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Ron Natalie
Guest





PostPosted: Fri Nov 03, 2006 11:01 pm    Post subject: Re: Virtual calls upon destruction Reply with quote

Bob Bell wrote:

Quote:

No; polymorphic calls don't work in destructors, for exactly the same
reason they don't work in constructors: when the destructor executes,
the dynamic type of the object is the same as the static type -- the
type whose destructor is executing.

Actually, polymorphic calls DO work for destructors, it's just that

the dynamic type of the object is that of the destructor being run.

This is an important significance. For example.

class A {
public:
void DF() { F(); }
virtual void F();
~A() { }
};

class B : public A {
public:
virtual void F();
~B() { DF(); }
};

class C : public B {
public:
virtual void F();
};

C c;

When C is destroyed:
~C() is called
~B() is called
A:Very HappyF() is invoked
B::F() is invoked by virtual call (not A::F or C::F)
~A() is called

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.