Boost logo

Boost Users :

From: Stefan Tarrant (stefan.tarrant_at_[hidden])
Date: 2006-06-28 10:29:12


When you use get() all bets are off. You are basically taking
responsibility for the ownership of the returned pointer yourself, which
is generally a bad idea. Use a const_pointer_cast<T> instead.

Here is my re-worked example:

#include <vector>
#include <iostream>
#include <boost/shared_ptr.hpp>

int main()
{
   std::vector<boost::shared_ptr<int> > x;
   x.push_back(boost::shared_ptr<int>(new int));
   *x.front() = 4;
   std::cout << "use count: " << x.front().use_count() << std::endl;
   boost::shared_ptr<const int> y = x.front();
   std::cout << "use count: " << x.front().use_count() << std::endl;
   std::cout << "use count: " << y.use_count() << std::endl;
   boost::shared_ptr<int> z = boost::const_pointer_cast<int>(y);
   std::cout << "use count: " << z.use_count() << std::endl;
   std::cout << "use count: " << y.use_count() << std::endl;
   x.pop_back();
   std::cout << *y << std::endl;
   std::cout << "use count: " << z.use_count() << std::endl;
   std::cout << "use count: " << y.use_count() << std::endl;

   y.reset();
   std::cout << "use count: " << z.use_count() << std::endl;
   std::cout << "use count: " << y.use_count() << std::endl;
}

And the output:

use count: 1
use count: 2
use count: 2
use count: 3
use count: 3
4
use count: 2
use count: 2
use count: 1
use count: 0
Press any key to continue . . .

-- 
Stefan Tarrant
Senior Project Engineer
C-CORE
Captain Robert A. Bartlett Building
Morrissey Road
St. John's, NL
Canada  A1B 3X5
Celebrating 30 years of Providing Innovative Engineering Solutions,
1975-2005.
Tel:               709 737-8372
General Fax:       709 737-4706
http://www.c-core.ca
---------------------------------------------------------------------
This communication (including all attachments) is intended solely for 
the use of the person or persons to whom it is addressed and should be 
treated as a confidential C-CORE communication.  If you are not the 
intended recipient, any use, distribution, printing, or copying of this 
email is strictly prohibited.  If you received this email in error, 
please immediately delete it from your system and notify the originator. 
  Your cooperation is appreciated.
Evan Drumwright wrote:
> Hi all,
> 
> I am having trouble with pointer ownership as a result of the const_cast 
> operator.  In particular, for the code below, when a const_cast is used to 
> create a const int pointer, the use count for x does not increase.  The 
> result of this is that when x.pop_back() is called, y is no longer valid 
> (though the use count for y remains 1).
> 
> The documentation for shared_ptr does not help here.  Any suggestions?
> 
> Thanks,
> Evan
> 
> 
> 
> #include <vector>
> #include <iostream>
> #include <boost/shared_ptr.hpp>
> 
> int main()
> {
> 	std::vector<boost::shared_ptr<int> > x;
> 	x.push_back(boost::shared_ptr<int>(new int));
> 	*x.front() = 4;
> 	std::cout << "use count: " << x.front().use_count() << std::endl;
> 	boost::shared_ptr<const int> y(const_cast<const int*>(x.front().get()));
> 	std::cout << "use count: " << x.front().use_count() << std::endl;
> 	std::cout << "use count: " << y.use_count() << std::endl;
> 	boost::shared_ptr<int> z(const_cast<int*>(y.get()));
> 	std::cout << "use count: " << z.use_count() << std::endl;
> 	std::cout << "use count: " << y.use_count() << std::endl;
> 	x.pop_back();
> 	std::cout << *y << std::endl;
> }
> 
> Output:
> use count: 1
> use count: 1
> use count: 1
> use count: 1
> use count: 1
> 0
> 
> 
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
> 

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net