Re: bad c++ (or bug 596)


Subject: Re: bad c++ (or bug 596)
From: Mike Nordell (tamlin@algonet.se)
Date: Wed May 10 2000 - 08:45:34 CDT


> This is the description of bug 596
>
> The following causes a core dump every time:
[snip]
> The funtion being called is cmdUnselectSelection, which is a method of
the
> classes FV_View and AP_View, where it is non-virtual, and it is a
> pure virtual function in AV_View. So this error means that we have
> committed a cardinal sin, and instantiated an abstract class.

I'd like to see _that_ compiler! :-)
No, it only means we've lost the _real_ class. An ABC can't be
instantiated as you know.

One of the few ways this can happen is if the concrete class gets sliced
by copying by value to AV_View. Let's close that possibility by enforcing
it. To the definition of class AV_View we add:

private:
    AV_View(const AV_View&); // no impl.
    void operator=(AV_View&); // no impl.

> The code making the bad call is
>
> m_pViewSelection->cmdUnselectSelection();
>
> And indeed, GDB tells us that m_pViewSelection is of type AV_View.

I suppose GDB gets this by looking at the vtable ptr. Could it be that
the concrete view have been destructed? In that case the dangling pointer
would contain the vtable ptr to the topmost baseclass, since it gets set
in that class' d'tor.

> Now, m_pViewSelection is declared in ap_UnixApp.h as AV_View. And the
> only place that it gets casted like a good C++ object is in
> getCurrentSelection().

To cast is often to display that the baseclass interface is incomplete.
If one really, really, have to cast, then, at least for debugging, one
might consider to use the compilers RTTI and use dynamic_cast instead of
static_cast.

#if defined (DEBUG) || defined (_DEBUG)
#define down_cast dynamic_cast
#else
#define down_cast static_cast
#endif

You don't get exceptions thrown if you try to cast a pointer which
underlying object is of an incompatible type, you just get a null ptr
back. Exceptions are only thrown by dynamic_cast if we try to cast to an
incompatible reference type. It would be pretty easy to assert on. If we
were to use it...

/Mike



This archive was generated by hypermail 2b25 : Wed May 10 2000 - 07:45:20 CDT