Alejandro Martinez | 18 Aug 22:09

shared_ptr custom deallocator - member function?

can i use some class's (a kind of factory) member be used as a deallocator 
in a shared_ptr?

like to do something like this:

boost::shared_ptr<mysqlpp::Connection> cp(sDbPool->grab(), 
boost::bind(&mysqlpp::ConnectionPool::release, sDbPool));

where sDbPool is a shared_ptr to the "factory/pool manager".

i tryed just that, but visual studio express 2008 keeps crashin on me. (the 
compiler crashes, not the program).

any ideas?
Peter Dimov | 18 Aug 22:29

Re: shared_ptr custom deallocator - member function?

Alejandro Martinez:
> can i use some class's (a kind of factory) member be used as a deallocator 
> in a shared_ptr?
>
> like to do something like this:
>
> boost::shared_ptr<mysqlpp::Connection> cp(sDbPool->grab(), 
> boost::bind(&mysqlpp::ConnectionPool::release, sDbPool));
>
> where sDbPool is a shared_ptr to the "factory/pool manager".

Assuming

    void ConnectionPool::release( Connection * pc );

try

    boost::bind( &ConnectionPool::release, sDbPool, _1 );

shared_ptr passes the raw pointer to its deleter, and the _1 passes it as 
the 'pc' parameter of 'release'. sDbPool is the implicit 'this'. 
Alejandro Martinez | 18 Aug 22:33

Re: shared_ptr custom deallocator - member function?

oh! silly me.

i blamed the compiler prematurely because of it crashing.

thank you very much :)

"Peter Dimov" <pdimov <at> pdimov.com> escribió en el mensaje 
news:00fe01c90171$3bb96db0$6507a80a <at> pdimov2...
> Alejandro Martinez:
>> can i use some class's (a kind of factory) member be used as a 
>> deallocator in a shared_ptr?
>>
>> like to do something like this:
>>
>> boost::shared_ptr<mysqlpp::Connection> cp(sDbPool->grab(), 
>> boost::bind(&mysqlpp::ConnectionPool::release, sDbPool));
>>
>> where sDbPool is a shared_ptr to the "factory/pool manager".
>
> Assuming
>
>    void ConnectionPool::release( Connection * pc );
>
> try
>
>    boost::bind( &ConnectionPool::release, sDbPool, _1 );
>
> shared_ptr passes the raw pointer to its deleter, and the _1 passes it as 
> the 'pc' parameter of 'release'. sDbPool is the implicit 'this'. 

(Continue reading)


Gmane