From 6013d994e2473619182920be5fe7767c807edeef Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Mon, 26 Nov 2012 11:20:12 -0800 Subject: [PATCH] STArray cannot use a vector of STObject's because bad things happen when you delete them. --- src/cpp/ripple/SerializedObject.cpp | 5 ++++- src/cpp/ripple/SerializedObject.h | 20 +++++++++++++------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/cpp/ripple/SerializedObject.cpp b/src/cpp/ripple/SerializedObject.cpp index 1991110449..bcece3584b 100644 --- a/src/cpp/ripple/SerializedObject.cpp +++ b/src/cpp/ripple/SerializedObject.cpp @@ -934,7 +934,7 @@ STArray* STArray::construct(SerializerIterator& sit, SField::ref field) throw std::runtime_error("Unknown field"); } - value.push_back(STObject(fn)); + value.push_back(new STObject(fn)); value.rbegin()->set(sit, 1); } @@ -1234,6 +1234,9 @@ BOOST_AUTO_TEST_SUITE(SerializedObject) BOOST_AUTO_TEST_CASE( FieldManipulation_test ) { + if (sfGeneric.isUseful()) + BOOST_FAIL("sfGeneric must not be useful"); + SField sfTestVL(STI_VL, 255, "TestVL"); SField sfTestH256(STI_HASH256, 255, "TestH256"); SField sfTestU32(STI_UINT32, 255, "TestU32"); diff --git a/src/cpp/ripple/SerializedObject.h b/src/cpp/ripple/SerializedObject.h index 910dc1c39b..99207fb72e 100644 --- a/src/cpp/ripple/SerializedObject.h +++ b/src/cpp/ripple/SerializedObject.h @@ -46,6 +46,8 @@ public: STObject(const std::vector& type, SerializerIterator& sit, SField::ref name) : SerializedType(name) { set(sit); setType(type); } + std::auto_ptr oClone() const { return std::auto_ptr(new STObject(*this)); } + static std::auto_ptr parseJson(const Json::Value& value, SField::ref name = sfGeneric, int depth = 0); virtual ~STObject() { ; } @@ -167,6 +169,10 @@ public: bool operator!=(const STObject& o) const { return ! (*this == o); } }; +// allow ptr_* collections of STObject's +inline STObject* new_clone(const STObject& s) { return s.oClone().release(); } +inline void delete_clone(const STObject* s) { boost::checked_delete(s); } + inline STObject::iterator range_begin(STObject& x) { return x.begin(); } inline STObject::iterator range_end(STObject &x) { return x.end(); } namespace boost @@ -180,12 +186,12 @@ namespace boost class STArray : public SerializedType, private IS_INSTANCE(SerializedArray) { public: - typedef std::vector vector; - typedef std::vector::iterator iterator; - typedef std::vector::const_iterator const_iterator; - typedef std::vector::reverse_iterator reverse_iterator; - typedef std::vector::const_reverse_iterator const_reverse_iterator; - typedef std::vector::size_type size_type; + typedef boost::ptr_vector vector; + typedef boost::ptr_vector::iterator iterator; + typedef boost::ptr_vector::const_iterator const_iterator; + typedef boost::ptr_vector::reverse_iterator reverse_iterator; + typedef boost::ptr_vector::const_reverse_iterator const_reverse_iterator; + typedef boost::ptr_vector::size_type size_type; protected: @@ -210,7 +216,7 @@ public: vector& getValue() { return value; } // vector-like functions - void push_back(const STObject& object) { value.push_back(object); } + void push_back(const STObject& object) { value.push_back(object.oClone()); } STObject& operator[](int j) { return value[j]; } const STObject& operator[](int j) const { return value[j]; } iterator begin() { return value.begin(); }