Re: MSVC 2003: ctor-try-block error: possible compiler bug
Kirill Sukhonosenko wrote:
struct IRefCounted
{
virtual void addRef() = 0;
virtual void release() = 0;
virtual ~IRefCounted() {};
^ unnecessary
};
struct ILogger : public IRefCounted
{
void virtual write(const char * text) = 0;
};
Why force implementors of ILogger to also implement IRefCounted? C++ has
multiple inheritance! In fact, I'd rather have made IRefCounted a concrete
class with a concrete implementation that invokes "delete this;" - a
virtual dtor will do the right thing!
template<typename T>
class CountingPtr
{
T * m_t;
public:
CountingPtr(T * t, bool addRef = true) : m_t(t)
{
assert(t!=0);
if(addRef)
t->addRef();
}
~CountingPtr()
{
if(m_t)
m_t->release();
}
Hmmm, weird. In the ctor, you assert() that m_t is not null and here you
check it again?
CountingPtr&operator=(const CountingPtr & rhs)
{
if(this == &rhs)
assert(false);
else
{
m_t = rhs.m_t;
m_t->addRef();
}
return *this;
}
This is very nontypical. Firstly, self-assignment, even though it rarely has
a proper use, can much easier be handled gracefully without asserting:
if(this!=&rhs)
{ ...do assignment... }
return *this;
Else, you simply overwrite the current value of m_t without first
deregistering from it (using release()). This is almost certainly wrong!
CountingPtr(const CountingPtr & rhs)
{
*this = rhs;
}
Note that here, you will also have to init m_t to zero before doing the
assignment!
Lastly, I'm missing a default ctor. The one generated by the compiler will
surely not do what you want, in particular not init m_t to zero.
I'm sorry if this doesn't directly address your problem, but browsing the
code those were some obvious mistakes I found and that I'd fix first. I
guess the rest will only turn out to be results of that.
Uli
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]