A
adebaene
Hello group,
There seems to be a bug int the interop layer in VC2005 when dealing
with certain pointer types (or values?)
Here is a repro case using Boost version 1.32 and C++/CLI :
using namespace System;
#pragma unmanaged
#include <boost/shared_ptr.hpp>
boost::shared_ptr<int> ReturnAPointer()
{
return boost::shared_ptr<int>();
}
#pragma managed
int main(array<System::String ^> ^args)
{
boost::shared_ptr<int> ptr=ReturnAPointer();
if (ptr)
{
System::Console::WriteLine("integer : "+ (*ptr));
}
}
ReturnAPointer clearly returns a NULL smart pointer (px==0). The
boolean operator for shared_ptr is :
typedef T * this_type::*unspecified_bool_type;
operator unspecified_bool_type() const // never throws
{
return px == 0? 0: &this_type:x;
}
Clearly, this operator should return an int** equal to 0, and indeed it
does so in the native layer.
However, back in managed code, the return value of boolean operator -
as shown by Visual 2005 - is 0xffffffff. Therefore, the WriteLine
function is called, which, of course causes an assertion in
shared_ptr:perator*.
The obvious workaround is to replace the test by :
if (ptr.get()!=NULL)
But it kind-of defeats the whole purpose of shared_ptr' boolean
operator, and it may be difficult to find those cases in existing code.
Before I report this to MS feedback, can anyone confirm the behaviour?
Any idea on a solution?
Thanks in advance
Arnaud
MVP - VC
PS : The problem is the same whatever the specialisation of shared_ptr
There seems to be a bug int the interop layer in VC2005 when dealing
with certain pointer types (or values?)
Here is a repro case using Boost version 1.32 and C++/CLI :
using namespace System;
#pragma unmanaged
#include <boost/shared_ptr.hpp>
boost::shared_ptr<int> ReturnAPointer()
{
return boost::shared_ptr<int>();
}
#pragma managed
int main(array<System::String ^> ^args)
{
boost::shared_ptr<int> ptr=ReturnAPointer();
if (ptr)
{
System::Console::WriteLine("integer : "+ (*ptr));
}
}
ReturnAPointer clearly returns a NULL smart pointer (px==0). The
boolean operator for shared_ptr is :
typedef T * this_type::*unspecified_bool_type;
operator unspecified_bool_type() const // never throws
{
return px == 0? 0: &this_type:x;
}
Clearly, this operator should return an int** equal to 0, and indeed it
does so in the native layer.
However, back in managed code, the return value of boolean operator -
as shown by Visual 2005 - is 0xffffffff. Therefore, the WriteLine
function is called, which, of course causes an assertion in
shared_ptr:perator*.
The obvious workaround is to replace the test by :
if (ptr.get()!=NULL)
But it kind-of defeats the whole purpose of shared_ptr' boolean
operator, and it may be difficult to find those cases in existing code.
Before I report this to MS feedback, can anyone confirm the behaviour?
Any idea on a solution?
Thanks in advance
Arnaud
MVP - VC
PS : The problem is the same whatever the specialisation of shared_ptr