Boost logo

Boost Users :

Subject: Re: [Boost-users] how to use boost::shared_ptr?
From: Krzysztof Czainski (1czajnik_at_[hidden])
Date: 2013-02-01 11:35:35


2013/2/1 young <yqin_99_at_[hidden]>

> I have a class member (boost::shared_ptr):
>
> class XYZ
> {
> private:
> boost::shared_ptr<int> m_pBuff;
> // other member
> ...
> public:
> void func(int n)
> {
> m_pBuff.reset(new int[n]); // or m_pBuff =
> boost::shared_ptr<int>(new int[n])
> ...;
> }
> // other functions
> ...
> }
>
> The func(n) will be called many times inside / outside the class. Can I use
> shared_ptr here like:
> m_pBuff.reset(new int[n]);
> or
> m_pBuff = boost::shared_ptr<int>(new int[n]);
>
> I tried both and get error said heap corruption
>

The error is passing an array to a shared_ptr. Take a look at shared_array
;-)
HTH, Kris



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