|
Prev: size of class having members as pointers to other class objects
Next: print line 3 of a file when current line is 7
From: Larry Lindstrom on 5 Jun 2008 07:24 Hi Folks: Developing on XP Home, VS 2008, C++. If I delete a derived class, which has a destructor, shouldn't the destructor of base class be called automatically? The derived class's destructor is being called, but the base class's destructor isn't. Do I have to delete the base class from the derived class's destructor? Thanks Larry
From: Alf P. Steinbach on 5 Jun 2008 07:29 * Larry Lindstrom: > > If I delete a derived class, which has a destructor, shouldn't > the destructor of base class be called automatically? Unless you have undefined behavior, a hang, or program termination. > The derived class's destructor is being called, but the base > class's destructor isn't. Do I have to delete the base class > from the derived class's destructor? No, never do that. Chances are you're doing something like Base* p = new Derived; delete p; where Base doesn't have a virtual destructor. This would be undefined behavior. Otherwise, post complete (can be compiled & run) but minimal example code. Cheers, & hth., - Alf -- A: Because it messes up the order in which people normally read text. Q: Why is it such a bad thing? A: Top-posting. Q: What is the most annoying thing on usenet and in e-mail?
From: Larry Lindstrom on 5 Jun 2008 16:04
Alf P. Steinbach wrote: > * Larry Lindstrom: >> >> If I delete a derived class, which has a destructor, shouldn't >> the destructor of base class be called automatically? > > Unless you have undefined behavior, a hang, or program termination. > > >> The derived class's destructor is being called, but the base >> class's destructor isn't. Do I have to delete the base class >> from the derived class's destructor? > > No, never do that. > > Chances are you're doing something like > > Base* p = new Derived; > delete p; > > where Base doesn't have a virtual destructor. > > This would be undefined behavior. > > Otherwise, post complete (can be compiled & run) but minimal example code. Thanks Alf: I bet that's it. I've called the functions needed to properly shut down the app, it was being hung waiting for a disconnect for the database, and I'll do the virtual destructor soon. Thanks Larry |