This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: problems with constructors


> >You are right, 'fullindex' is of a base class type.
> >
> >Strictly speaking, you are right; it is not necessarily true that the
> >pointer to the base class is the same as the pointer to the subclass.
> >However:
> >
> >(a) I should still be able to do 'delete fullindex;', but this statement
> >crashes. If I link in 'Electric fence', it tells me 'fullindex' is not a
> >malloc'ed pointer. The difference in pointers is a symptom of the problem;
> >the real problem is that 'delete fullindex;' crashes.
> 
> Yes, you should be able to delete fullindex, *provided* the base class
> (the type fullindex is declared to point to) has a virtual destructor.
> (Does it? If not, I'd expect the delete to have nasty consequences.)

<Cough> Oops. You are absolutely right. The base class didn't have a
virtual destructor, and now that it has one the problem is gone. Sorry
everyone for wasting time.

BTW, this is a nasty gotcha. Is there a way to let the compiler warn me
about this?

> I'm not familiar with Electric Fence -- from your description of the
> symptoms, my first guess would be that EF only knows about C and
> doesn't understand polymorphism.

Electric fence is a replacement library for malloc and free that does
extensive validity checking.  Very useful, although in this case perhaps
misleading.

>                                * * * * *
>       "Screw up your courage. You've screwed up everything else."

Yeah, yeah, thank you. :-)


-- 
Kees van Reeuwijk, Delft University of Technology
http://www.pds.twi.tudelft.nl/~reeuwijk


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]