Boost logo

Boost Users :

From: Xu, Peng (peng.xu_at_[hidden])
Date: 2007-03-20 21:49:20


-----Original Message-----
From: boost-users-bounces_at_[hidden]
[mailto:boost-users-bounces_at_[hidden]] On Behalf Of Joe Gottman
Sent: Tuesday, March 20, 2007 6:10 PM
To: boost-users_at_[hidden]
Subject: Re: [Boost-users] [shared_ptr] why segfault?

Xu, Peng wrote:
> greetings,
>
> my test code is as follows:
> "
> int k = 128;
> std::vector<boost::shared_ptr<int> > v(k);
> for(unsigned int i = 0; i < k; ++i)
> {
> v[i].reset(&k);
> // v[i].reset(new int(0));
> }
>

    v[i].reset(&k) assumes that &k was allocated via a call to new.
Since it wasn't, you will eventually get a segfault when the shared
pointer at v[i] attempts to delete &k. To get around this, use the
version of reset() that takes a pointer and a deleter object.

struct null_deleter {
      void operator()(const void *) {} // Do-nothing operator()
};

int k = 128;
std::vector<boost::shared_ptr<int> > v(k);
for (unsigned int i = 0; i < k; ++i) {
      v[i].reset(&k, null_deleter());
}

Joe Gottman

That helps. Thanks a lot!

_______________________________________________
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