Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > shared_ptr to incomplete type, undefined behavior?

Reply
Thread Tools

shared_ptr to incomplete type, undefined behavior?

 
 
Alan Johnson
Guest
Posts: n/a
 
      05-13-2006
From the standard 5.3.5/5:
"If the object being deleted has incomplete class type at the point of
deletion and the complete class has a non-trivial destructor or a
deallocation function, the behavior is undefined."

In the program below, what constitutes "point of deletion"? Presumably,
since delete is called somewhere within shared_ptr's template, the point
of deletion is the same place at which the template is instantiated.

Does the following program therefore exhibit undefined behavior, or am I
missing something? (Does this generalize to all cases in which a
shared_ptr is created from a forward reference?)


#include <iostream>
#include <boost/shared_ptr.hpp>

// Forward reference.
class A ;

// A has incomplete type here.
boost::shared_ptr<A> pa ;

class A
{
public:
A() { std::cout << "A()" << std::endl ; }
~A() { std::cout << "~A()" << std::endl ; }
} ;

int main()
{
pa.reset(new A) ;
}

--
Alan Johnson
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      05-13-2006
* Alan Johnson:
> From the standard 5.3.5/5:
> "If the object being deleted has incomplete class type at the point of
> deletion and the complete class has a non-trivial destructor or a
> deallocation function, the behavior is undefined."
>
> In the program below, what constitutes "point of deletion"? Presumably,
> since delete is called somewhere within shared_ptr's template, the point
> of deletion is the same place at which the template is instantiated.
>
>
> Does the following program therefore exhibit undefined behavior, or am I
> missing something? (Does this generalize to all cases in which a
> shared_ptr is created from a forward reference?)
>
>
> #include <iostream>
> #include <boost/shared_ptr.hpp>
>
> // Forward reference.
> class A ;
>
> // A has incomplete type here.
> boost::shared_ptr<A> pa ;
>
> class A
> {
> public:
> A() { std::cout << "A()" << std::endl ; }
> ~A() { std::cout << "~A()" << std::endl ; }
> } ;
>
> int main()
> {
> pa.reset(new A) ;
> }


If you move the definition of class A to a separate file, plus a factory
function f that produces a raw pointer, and use f in 'main' instead of
direct use of 'new', then yes, in that case you would have UB --
except that the Boost code checks for this, so it won't compile:

// verify that types are complete for increased safety

template<class T> inline void checked_delete(T * x)
{
// intentionally complex - simplification causes regressions
typedef char type_must_be_complete[ sizeof(T)? 1: -1 ];
(void) sizeof(type_must_be_complete);
delete x;
}

However, as your code is it compiles, at least with MSVC 7.1, and so
(what an argument!) I think the instantiation of the template is at the
point of actual usage in 'main'.

Note that boost::shared_ptr provides a way to avoid UB even when you do
have an incomplete type, by passing a deleter function to the
constructor (that's why boost::shared_ptr is great for the PIMPL idiom).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
shared_ptr and incomplete types mike.polyakov@gmail.com C++ 7 11-29-2007 01:49 PM
undefined vs. undefined (was: new Array() vs []) VK Javascript 45 09-12-2006 05:26 PM
#include <boost/shared_ptr.hpp> or #include "boost/shared_ptr.hpp"? Colin Caughie C++ 1 08-29-2006 02:19 PM
undefined behavior or not undefined behavior? That is the question Mantorok Redgormor C Programming 70 02-17-2004 02:46 PM
error with boost::shared_ptr<T> with incomplete T, VC7.1 ? Philippe Guglielmetti C++ 4 10-09-2003 05:30 PM



Advertisments