473,507 Members | 2,374 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

shared_ptr to incomplete type, undefined behavior?

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
May 13 '06 #1
1 3821
* 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?
May 13 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3129
by: Philippe Guglielmetti | last post by:
I just ported old (VC6) working code to VC7.1 and have trouble with something like: class A; // forward typedef boost::smart_ptr<A> Aptr; class B{ Aptr a; virtual ~B(); // implemented...
2
7209
by: Dennis Jones | last post by:
Hello, Given something like: boost::shared_ptr<T> t( new T() ); What is the best (correct?) way to dereference the pointer? The following two methods work. Is there a difference?
7
1731
by: Mantorok Redgormor | last post by:
An lvalue is an expression with an object type or an incomplete type other than void;53) if an lvalue does not designate an object when it is evaluated, the behavior is undefined. When would...
6
4174
by: Eric Smith | last post by:
Is a structure containing an incomplete array as its last element (per paragraph 2 of section 6.7.2.1 of ISO/IEC 9899:1999 (E)) itself an incomplete type? That appears to be indicated by paragraph...
2
6173
by: aegis | last post by:
sizeof could not possibly evaluate to some size for a type, unless two things for a type occur: a) the type is complete and sizeof will evaluate to appropriate size b) type is not complete and...
2
2004
by: dave_dp | last post by:
Hi folks, I'm interested as to what extent using incomplete types doesn't result in a undefined behavior, more generally it touches the usage of incomplete types.. for example, it is stated that...
2
6175
by: Kai-Uwe Bux | last post by:
Does the following have undefined behavior? struct X; struct Y { X * x_ptr; Y ( void ) : x_ptr ( 0 )
5
4244
by: mike.polyakov | last post by:
I have trouble understanding incomplete types and their interplay with shared_ptr. Consider the following code, composed of two source files and one header: ...
0
1565
by: phlip | last post by:
Nick Keighley wrote: CC'd to the correct newsgroup. Yes, the destructor of the shared pointer will delete the object. Then its former address will convert to a reference. At some point -...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7319
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7031
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5623
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5042
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1542
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
412
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.