VC2005 and boost::shared_ptr

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::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::blush: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
 
T

Tom Widmer [VC++ MVP]

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.

Well, it's actually an "int* boost::shared_ptr<int>::*" (e.g. a pointer
to boost::shared_ptr said:
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::blush:perator*.

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
 
A

Arnaud Debaene

Tom said:
Well, it's actually an "int* boost::shared_ptr<int>::*" (e.g. a
pointer to boost::shared_ptr<int> member variable of type int*).

Yep, you're right. So much for worknig late in the night on that kind of
stuff ;-)
This might be a problem with the handling of pointer to members. You
could try fiddling with the pointer-to-member related compiler
switches.

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/Produ...edbackid=b8d81704-ba8e-46d8-8f89-99cabee9df90

Arnaud
MVP - VC
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top