A very stupid bug...

  • Thread starter Alf P. Steinbach
  • Start date
A

Alf P. Steinbach

At one point I became convinced that the compiler generated incorrect
destruction code for multiple inheritance.

Oh well.

I finally found it:

virtual ~RawPtrHolder()
{
std::auto_ptr< Type >( myPtr ); // Destroy via std::auto_ptr for access.
}

Uh oh.

Can you see what I did wrogn?


Cheers,

- Alf
 
C

cpp4ever

At one point I became convinced that the compiler generated incorrect
destruction code for multiple inheritance.

Oh well.

I finally found it:

virtual ~RawPtrHolder()
{
std::auto_ptr< Type >( myPtr ); // Destroy via std::auto_ptr for
access.
}

Uh oh.

Can you see what I did wrogn?


Cheers,

- Alf

Tried to free invalid memory because myPtr had not been
reset/initialised to NULL? That would be nasty.

Using std::auto_ptr in a constructor to simplify the throwing
of exceptions, maybe, but I'm not sure I'd ever use std::auto_ptr
like that.

JB
 
A

Alf P. Steinbach

Tried to free invalid memory because myPtr had not been
reset/initialised to NULL? That would be nasty.

Using std::auto_ptr in a constructor to simplify the throwing
of exceptions, maybe, but I'm not sure I'd ever use std::auto_ptr
like that.

Eric J. Holtman had it mostly right (else-thread).

The problem is that the statement is a declaration, of a variable 'myPtr',
instead of constructing a temporary std::auto_ptr instance; it's known as "the
most vexed parse of C++", that anything that syntactically can be treated as a
declaration is treated as a declaration...

My fix was to write

std::auto_ptr<Type>( myPtr+0 )

:)


Cheers,

- Alf
 
C

cpp4ever

Eric J. Holtman had it mostly right (else-thread).

The problem is that the statement is a declaration, of a variable
'myPtr', instead of constructing a temporary std::auto_ptr instance;
it's known as "the most vexed parse of C++", that anything that
syntactically can be treated as a declaration is treated as a
declaration...

My fix was to write

std::auto_ptr<Type>( myPtr+0 )

:)


Cheers,

- Alf

DOH!!! Of course that's the problem. Serves you right for trying to be
too clever by half. I'd have probably declared a temporary variable for
the std::auto_ptr, if only because it's easier to read. Doesn't A.L.F
stand for alien life form?

JB
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top