[C++-sig] Attempt to Return Dangling Reference Error -- What'sthe solution?

Bilokon, Paul paul.bilokon at lehman.com
Wed Sep 3 11:27:02 CEST 2008


Hi Dave,

Thought I should give more background. The library I'm wrapping is using
a custom smart pointer, which is really unfortunate. It would be
difficult (though desirable) to replace these custom shared pointers
with shared_ptr. Therefore I'm trying to provide a mechanism for calling
the functions expecting CustomPtr with a CustomPtr-to-copy by calling
clone and wrapping the copy into CustomPtr, wherever this has reasonable
semantics. Therefore I cannot wrap the return of clone into shared_ptr.
Is there any way to cheat the dangling reference detection mechanism?
(Of course, shared_ptr does not do release().)

Many thanks,
Paul

-----Original Message-----
From: Bilokon, Paul 
Sent: Wednesday, September 03, 2008 9:09 AM
To: 'Development of Python/C++ integration'
Subject: RE: [C++-sig] Attempt to Return Dangling Reference Error --
What'sthe solution?

Hi Dave,

Thanks a lot. Changing clone's signature to...

    virtual boost::shared_ptr<Animal> clone() const

did the trick. However, since I'm wrapping an existing library, I'm
trying to find a non-intrusive solution. I'd rather keep the current
signature of clone. Are there any other options?

Many thanks,
Paul

-----Original Message-----
From: c++-sig-bounces+paul.bilokon=lehman.com at python.org
[mailto:c++-sig-bounces+paul.bilokon=lehman.com at python.org] On Behalf Of
David Abrahams
Sent: Friday, August 29, 2008 3:53 PM
To: Development of Python/C++ integration
Subject: Re: [C++-sig] Attempt to Return Dangling Reference Error --
What'sthe solution?


on Fri Aug 29 2008, "Bilokon, Paul" <paul.bilokon-AT-lehman.com> wrote:

> Hi,
>
> I'm looking at this:
>
> http://www.boost.org/doc/libs/1_36_0/libs/python/doc/v2/faq.html#dangl
> in
> g
>
> ('I'm getting the "attempt to return dangling reference" error. What 
> am I doing wrong?')
>
> I understand the statement of the problem. However, suppose I need to 
> hold on to the new Python object and the C++ object contained therein.
> How do I achieve that?
>
> Let me give you the specific context.
>
>
> 	void takesAnimalRawPtr(Animal * animalRawPtr)
> 	{
> 	    cout << "This is " << animalRawPtr->whatIsIt() << endl;
> 	    Animal * clone = animalRawPtr->clone();
> 	    cout << "Now this is " << clone->whatIsIt() << endl;
> 	    delete clone;
> 	}

I suggest you make your clone() functions return a
boost::shared_ptr<Animal>.  That would make all your problems disappear.

HTH,

--
Dave Abrahams
BoostPro Computing
http://www.boostpro.com
_______________________________________________
C++-sig mailing list
C++-sig at python.org
http://mail.python.org/mailman/listinfo/c++-sig
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

This message is intended only for the personal and confidential use of the designated recipient(s) named above.  If you are not the intended recipient of this message you are hereby notified that any review, dissemination, distribution or copying of this message is strictly prohibited.  This communication is for information purposes only and should not be regarded as an offer to sell or as a solicitation of an offer to buy any financial product, an official confirmation of any transaction, or as an official statement of Lehman Brothers.  Email transmission cannot be guaranteed to be secure or error-free.  Therefore, we do not represent that this information is complete or accurate and it should not be relied upon as such.  All information is subject to change without notice.




More information about the Cplusplus-sig mailing list