2

UPDATE the belowmentioned bug is fixed in VS2012, and noncopyable works as epected

This is both a question and a way to provide information / warn others so they don't fall into the same trap as I did: it seems that using a noncopyable base class (like the one in boost) has no effect in exported classes when using the MS compiler. This is a known bug to MS but I doubt there are a lot of programmers that know of it. As one can imagine, this can produce extremely nasty bugs because it allows writing code that should not even compile. Example (code for noncopyable class here:)

a typical header file in a dll project, compile with /D EXPORT_IT:

#ifdef EXPORT_IT
  #define mydll __declspec( dllexport )
#else
  #define mydll __declspec( dllimport )
#endif    

class mydll CantCopyMe : private noncopyable
{
public:
  CantCopyMe();
  ~CantCopyMe();
};

mydll CantCopyMe MakeIt();

the source file:

#include <iostream>

CantCopyMe::CantCopyMe()
{
  std::cout << "constructor" << std::endl;
}

CantCopyMe::~CantCopyMe()
{
  std::cout << "destructor" << std::endl;
}

CantCopyMe MakeIt()
{
  CantCopyMe x;
  return x; //oops... this sould not compile nor link but it does
}

the application:

int main()
{
  CantCopyMe x( MakeIt() );
}

the output:

constructor
destructor
destructor

1 constructor, 2 destructors called. Imagine the problems when the class effectively contains resources.

edit usage cases that do compile but should not:

CantCopyMe MakeIt()
{
  CantCopyMe x;
  return x;
}

void DoIt( CantCopyMe x )
{
  x.Foo();
}

void SomeFun()
{
  CantCopyMe x;
  DoIt( x );
}

other cases: CantCopyMe MakeIt() { return CantCopyMe(); //fatal error C1001 }

CantCopyMe GenerateIt()
{
  CantCopyMe x;
  return x;
}

CantCopyMe MakeIt()
{
  return GenerateIt(); //fatal error C1001
}

CantCopyMe MakeIt()
{
  CantCopyMe x;
  return CantCopyMe( x ); //fatal error C1001 + cl crashes
}

void DoSomething()
{
  CantCopyMe x;
  CantCopyMe y = x; //fatal error C1001 + cl crashes
}  

Questions:

  1. The KB article mentions a fix in an upcoming release. Can anyone check if this is fixed in VS2010 already (or possibly with a Visual Studio 11 preview)?

  2. Is there any workaround to trigger any kind of error? I tried (ab)using the fact that writing return CantCopyMe() triggers an internal compiler error but, I couldn't find a way to conditionally trigger it only when compiling a function like MakeIt above. Putting static_assert in the noncopyable's copy constructor also doesn't cut it since the compiler will always compile it even if it doesn't get invoked.

stijn
  • 34,664
  • 13
  • 111
  • 163
  • Does this happen to returning an object only or have you tried other contexts? Maybe your compiler is applying NRVO without checking for an accessible copy constructor (which is non-conforming). – Luc Danton Sep 20 '11 at 09:38
  • @Luc Danton will edit and add some other cases, not sure if that is what you mean with 'other contexts'; could indeed be a rather crippled form of NRVO being applied here (no copy constructor called, but destructor is).. – stijn Sep 20 '11 at 10:01

2 Answers2

2

To answer 1 (for VS2010), I just tried it in VS2010 (with SP1) and it compiles just fine, which means it has not been fixed. Unfortunately I do not have 2011 to test

To 2. I guess one way to do it would be to:

  • no longer derive from noncopyable
  • declare the copy ctor and assignment operator as private in CantCopyMe without providing an implementation)
class CantCopyMe 
{
public:
   //omitted for brevity...
private:
    CantCopyMe( const CantCopyMe& );
    const CantCopyMe& operator=( const CantCopyMe& );
};

With this done you avoided the dangerous situations you describe and it should work with VS2008 also. And you solve the problem in the right place, that is in the non-copyable class declaration.

Andreas Haferburg
  • 5,189
  • 3
  • 37
  • 63
ds27680
  • 1,993
  • 10
  • 11
  • thanks. Declaring the copy ctor and assignment operator as private in CantCopyMe is what I used to do earlier (through a macro), but it feels like a DRY violation. I like inheriting noncopyable more beacuse it's a clear statement.. – stijn Sep 20 '11 at 11:31
  • Hi stjin. Yes, you are probably right, it doesn't feel right and it would be nice if it would just work. But given the fact that the problem exists I see no other better solution unfortunately. I guess you could still derive from noncopyable and also use the macro as before to declare the copy ctor and assignment operator of CantCopyMe private. At a point in time when the bug is fixed you could simply redefine the macro to be empty (or simply remove the macro and see where you get errors and remove it wherever it is used). – ds27680 Sep 21 '11 at 07:05
1

I just ran into this same issue in a slightly different situation: I have a DLL exported class, which was given a member that is non-copyable. The DLL exported class has no explicit copy-constructor and has a Copy method that returns a copy of itself on the heap. When the non-copyable member was added, there was no compiler error but a nasty runtime error. I tracked it down to the __declspec(dllexport) and found if I removed it I got the expected and correct compiler error preventing the copy. Consider this minimal example:

#define API __declspec(dllexport)

class Inner
{
public:
    Inner() {}

private:
    Inner(const Inner&) {}
    Inner& operator=(const Inner&) { return *this; }
};

class API Outer
{
private:
    Inner i;

public:
    virtual Outer* Copy()
    {
        return new Outer(*this);
    }
};

When I compile this with the latest VS2010 I get: error C4716: 'Outer::Copy' : must return a value. If I change Copy() to this:

virtual Outer* Copy()
{
    Outer* copy = new Outer(*this);
    return copy;
}

I now get only an odd warning: warning C4700: uninitialized local variable 'copy' used, and a nasty crash at runtime. Finally, give this a try:

virtual Outer* Copy()
{
    Outer tmp(*this);
    return nullptr;
}

The compiler will reliably crash! This is on VS2010 SP1, C++ Compiler Version 16.00.40219.01 for 80x86.

Chadwick
  • 944
  • 12
  • 15