Subject: Re: [Boost-bugs] [Boost C++ Libraries] #3604: Access violation on diamond inheritance
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2009-11-29 07:29:18
#3604: Access violation on diamond inheritance
----------------------------------+-----------------------------------------
Reporter: kondo@⦠| Owner: ramey
Type: Bugs | Status: closed
Milestone: Boost 1.41.0 | Component: serialization
Version: Boost 1.40.0 | Severity: Problem
Resolution: invalid | Keywords:
----------------------------------+-----------------------------------------
Changes (by ramey):
* status: assigned => closed
* resolution: => invalid
Comment:
Making a small change in your example illustrates the problem here.
Turns out you can downcast a pointer from a virtual base class. It
makes sense when you think about it. The serialization library does
this at runtime through an intermediate void * so it doesn't detect
the error at compile time. And the MSVC compiler doesn't detect it
unless it's done with a static cast. It turns out that the vbc data
is "attached" to the first subclass. So the problem only occurs when
we try to use the second - sub2.
If you make the following change in your program, and try to compile
I think the problem will be obvious. (note to self, always use
static_cast rather than implicit pointer conversions !!!)
{{{
// Serialize pTarget via VBase pointer.
{
std::ofstream ofs("outtarget.xml");
boost::archive::xml_oarchive oa(ofs);
assert(ofs);
Target* pTarget = new Target;
VBase * pVBase = static_cast<VBase *>(pTarget);
Target* pTarget2 = static_cast<Target *>(pVBase);
oa << boost::serialization::make_nvp("pVBase", pVBase);
}
{
std::ifstream ifs("outtarget.xml");
assert(ifs);
boost::archive::xml_iarchive ia(ifs);
VBase * pVBase;
ia >> boost::serialization::make_nvp("pVBase", pVBase);
}
}}}
PS I did in fact find some problems with void cast registry - but I don't
think they're related to this. After I fixed them, I still had the
problem.
Robert Ramey
-- Ticket URL: <https://svn.boost.org/trac/boost/ticket/3604#comment:2> Boost C++ Libraries <http://www.boost.org/> Boost provides free peer-reviewed portable C++ source libraries.
This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:01 UTC