PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Controlling/releasing ownership in shared_ptr
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Controlling/releasing ownership in shared_ptr

Réponse
 
LinkBack Outils de la discussion
Vieux 16/01/2008, 17h54   #1
digz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Controlling/releasing ownership in shared_ptr

In the code below , a simplified version
I need to pass a smart pointer to function f which I control .I
cannot use std::auto_ptr as a parameter to f because
it releases ownership on copy construction and I still need the
pointer after f returns.( may be I could return the auto_ptr
again ?? )

after f does its stuff , i need to pass it to g which expects a raw
pointer ( I cannot change that API )
I know g does maintain its own ptr_deque which takes care of
ownership etc.. ,

I am forced to do a get() on the shared_ptr ( instead of an ideal
release() on auto_ptr which would free the ptr from smart
management )
Now shared_ptr and ptr_deque desturctors cause a double delete
leading to UB.
How do i workaround this problem ?

Thx
Digz

#include<boost/shared_ptr.hpp>
#include<boost/ptr_container/ptr_deque.hpp>

boost::ptr_deque<int> l;

void f(boost::shared_ptr<int> t = boost::shared_ptr<int>())
{}

void g(int* i){
l.push_back(i);
}

int h(){
boost::shared_ptr<int> i(new int);
f(i); //need to pass aroung i, so cant use std::auto_ptr
g(i.get()); //cant release from boost::shared_ptr ownership
}

int main(){
h();
}
  Réponse avec citation
Vieux 16/01/2008, 18h35   #2
Alf P. Steinbach
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Controlling/releasing ownership in shared_ptr

* digz:
> In the code below , a simplified version
> I need to pass a smart pointer to function f which I control .I
> cannot use std::auto_ptr as a parameter to f because
> it releases ownership on copy construction and I still need the
> pointer after f returns.( may be I could return the auto_ptr
> again ?? )
>
> after f does its stuff , i need to pass it to g which expects a raw
> pointer ( I cannot change that API )
> I know g does maintain its own ptr_deque which takes care of
> ownership etc.. ,
>
> I am forced to do a get() on the shared_ptr ( instead of an ideal
> release() on auto_ptr which would free the ptr from smart
> management )
> Now shared_ptr and ptr_deque desturctors cause a double delete
> leading to UB.
> How do i workaround this problem ?


>
> #include<boost/shared_ptr.hpp>
> #include<boost/ptr_container/ptr_deque.hpp>
>
> boost::ptr_deque<int> l;


"l" is not a good name because it's easily confused with "1".


> void f(boost::shared_ptr<int> t = boost::shared_ptr<int>())
> {}
>
> void g(int* i){
> l.push_back(i);
> }
>
> int h(){
> boost::shared_ptr<int> i(new int);
> f(i); //need to pass aroung i, so cant use std::auto_ptr
> g(i.get()); //cant release from boost::shared_ptr ownership
> }


You can use std::auto_ptr for exception safety and ownership transfer.

Off-the-cuff, fix errors if any:

void f( int& ) {}

void transferOwnershipToG( std::auto_ptr<int> p )
{
int* pRaw = p.get();
p.release();
g( pRaw );
}

int h()
{
std::auto_ptr<int> p( new int );
f( *p ); // If f throws, std::auto_ptr deallocates.
transferOwnershipToG( p );
}

Or, with the current boost::shared_ptr interface you can release a
boost::shared_ptr by replacing its delete function. I think the
function to gain access to the deleter is named get_deleter. However,
this could be a bit dangerous, because with a shared_ptr at hand one
expects that it will handle deallocation and can be copied and stored
safely, which will not be the case in your code.

Cheers, & hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
  Réponse avec citation
Vieux 17/01/2008, 10h04   #3
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Controlling/releasing ownership in shared_ptr

On Jan 16, 7:35 pm, "Alf P. Steinbach" <al...@start.no> wrote:
> * digz:


[...]
> Off-the-cuff, fix errors if any:


> void f( int& ) {}


> void transferOwnershipToG( std::auto_ptr<int> p )
> {
> int* pRaw = p.get();
> p.release();
> g( pRaw );
> }


Generally, I would do the release after the call to g(), i.e.:
g( p.get() ) ;
p.release() ;
Only if the call to g() succeeds can I be sure that it has taken
over ownership. Otherwise, you can just call g() directly:
g( p.release() ) ;

(Obviously, this depends partially on g(). Normally, in the
absence of other documentation, I would expect transactional
semantics, which means that if g() returns via an exception,
nothing has changed, and I am still responsible for the object.
But it's something I'd verify anyway.)

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
  Réponse avec citation
Vieux 17/01/2008, 10h23   #4
Alf P. Steinbach
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Controlling/releasing ownership in shared_ptr

* James Kanze:
> On Jan 16, 7:35 pm, "Alf P. Steinbach" <al...@start.no> wrote:
>> * digz:

>
> [...]
>> Off-the-cuff, fix errors if any:

>
>> void f( int& ) {}

>
>> void transferOwnershipToG( std::auto_ptr<int> p )
>> {
>> int* pRaw = p.get();
>> p.release();
>> g( pRaw );
>> }

>
> Generally, I would do the release after the call to g(), i.e.:
> g( p.get() ) ;
> p.release() ;


Yup. I had a feeling there was something.


> Only if the call to g() succeeds can I be sure that it has taken
> over ownership. Otherwise, you can just call g() directly:
> g( p.release() ) ;


Yup again -- otherwise no reason for having the transferOwnershipToG
wrapper.


> (Obviously, this depends partially on g(). Normally, in the
> absence of other documentation, I would expect transactional
> semantics, which means that if g() returns via an exception,
> nothing has changed, and I am still responsible for the object.
> But it's something I'd verify anyway.)


Code above assumed failure reporting via exceptions.

Cheers,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 07h48.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10693 seconds with 12 queries