Opened 20 years ago

Closed 18 years ago

#114 closed Bugs (Fixed)

VC doesn't produce error no code either

Reported by: nobody Owned by: Peter Dimov
Milestone: Component: smart_ptr
Version: None Severity:
Keywords: Cc:

Description

When Using shared_ptr as shown below, the 
compiler should not compile but show an error 
instead. However MS Visual Studio compiles 
without errors but does not generate any code!

The following example shows the problem:

#include <boost/shared_ptr.hpp>

class D { /* ... */ };

void test()
{
  D *pD = new D;
  // ...
  boost::shared_ptr<D> dPtr;
  // ...
  // AFAIK the following should not compile
  dPtr = pD; // Problem with compiler!

  // what the developer intended actually was:
  // dPtr = boost::shared_ptr<D> (pD);
  // which works as expected
}

Class D dtor is never called and memory leaks. The 
Visual Studio compiler didn't even create code for 
the assignment.

This is a really dangerous situation, since it is a 
source of serious bugs which should be avoided 
during compile time! 
I really like the shared_ptr, however this compiler 
problem makes them very risky to use.

Looks like the compiler is seriously broken.
Is this a known problem?
Is there a reasonable workaround?

Change History (2)

comment:1 by nobody, 20 years ago

Logged In: NO 

After some further research it looks like msvc gets 
confused by assignment operator

template<typename T> class shared_ptr
{
...
public:
...
    template<typename Y>
    shared_ptr & operator=(shared_ptr<Y> const & r)
...

If this assignment operator is not declared, then msvc 
correctly produces an error when compiling dPtr= pD;

I locally fixed this by declaring an additional _private_ 
assignment operator (which does not get implemented).

template<typename T> class shared_ptr
{
private:
...
  // FIX:
    this_type &operator=(T* rhs);
...

This way dPtr = pD doesn't compile either, which is 
what I expect.

comment:2 by Peter Dimov, 18 years ago

Status: assignedclosed
Note: See TracTickets for help on using tickets.