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

David Abrahams dave at boostpro.com
Fri Aug 29 16:52:43 CEST 2008


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#danglin
> 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



More information about the Cplusplus-sig mailing list