shared_array double free problem

Here is the compilable code: #include <boost/shared_array.hpp> #include <iostream> template <typename T> class VectorView; template <typename T> inline std::ostream& operator << (std::ostream& os, const VectorView<T>& VOut) { os<<VOut.size()<<std::endl; for (int i=0; i<VOut.size() && os.good(); ++i){ os<<VOut(i)<<""; os<<std::endl; } return os; } template<typename T> class VectorView { private: int _size; boost::shared_array<T> _vp; public: VectorView(T* ptr, int size) { _size=size; _vp=boost::shared_array<T>(ptr); } T operator () (int pos) const { return _vp[pos]; } int size() const { return _size; } ~VectorView() { } }; int main (void) { double a[]={1,2,3,4,5,6,7}; VectorView<double>V(a,4); std::cout<<V; return 0; } Any workaround?

Jack Nguyen wrote:
Here is the compilable code:
<snip>
VectorView(T* ptr, int size) { _size=size; _vp=boost::shared_array<T>(ptr); }
<snip>
int main (void) { double a[]={1,2,3,4,5,6,7}; VectorView<double>V(a,4); std::cout<<V; return 0; }
Any workaround?
shared_array doesn't copy arrays (that's half the point of using it). You really don't want to delete arrays with automatic storage, so don't pass them to shared_array. Ben.
participants (2)
-
Ben Hutchings
-
Jack Nguyen