Connecting Tech Pros Worldwide Forums | Help | Site Map

VC2005 and boost::shared_ptr

adebaene@club-internet.fr
Guest
 
Posts: n/a
#1: Apr 7 '06
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::px;
}

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::operator*.

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


Tom Widmer [VC++ MVP]
Guest
 
Posts: n/a
#2: Apr 7 '06

re: VC2005 and boost::shared_ptr


adebaene@club-internet.fr wrote:
[color=blue]
> 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::px;
> }
>
> Clearly, this operator should return an int** equal to 0, and indeed it
> does so in the native layer.[/color]

Well, it's actually an "int* boost::shared_ptr<int>::*" (e.g. a pointer
to boost::shared_ptr<int> member variable of type int*).
[color=blue]
> 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::operator*.[/color]

This might be a problem with the handling of pointer to members. You
could try fiddling with the pointer-to-member related compiler switches.

Tom
Arnaud Debaene
Guest
 
Posts: n/a
#3: Apr 8 '06

re: VC2005 and boost::shared_ptr


Tom Widmer [VC++ MVP] wrote:
[color=blue]
> Well, it's actually an "int* boost::shared_ptr<int>::*" (e.g. a
> pointer to boost::shared_ptr<int> member variable of type int*).[/color]

Yep, you're right. So much for worknig late in the night on that kind of
stuff ;-)[color=blue]
>[color=green]
>> 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::operator*.[/color]
>
> This might be a problem with the handling of pointer to members. You
> could try fiddling with the pointer-to-member related compiler
> switches.[/color]

I've tried all the possible combinations of vmm/vms/vmv and vmb/vmg compiler
switches with no luck.

I've opened a bug report here :
http://lab.msdn.microsoft.com/Produc...9-99cabee9df90

Arnaud
MVP - VC


Closed Thread