diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index afe53541..7e908998 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -269,8 +269,8 @@ public: o.members = (Member*)allocator.Realloc(o.members, oldCapacity * sizeof(Member), o.capacity * sizeof(Member)); } } - o.members[o.size].name = name; - o.members[o.size].value = value; + o.members[o.size].name.RawAssign(name); + o.members[o.size].value.RawAssign(value); o.size++; return *this; } @@ -396,7 +396,7 @@ int z = a[0u].GetInt(); // This works too. RAPIDJSON_ASSERT(IsArray()); if (data_.a.size >= data_.a.capacity) Reserve(data_.a.capacity == 0 ? kDefaultArrayCapacity : data_.a.capacity * 2, allocator); - data_.a.elements[data_.a.size++] = value; + data_.a.elements[data_.a.size++].RawAssign(value); return *this; } diff --git a/test/unittest/valuetest.cpp b/test/unittest/valuetest.cpp index 1578c035..60222a94 100644 --- a/test/unittest/valuetest.cpp +++ b/test/unittest/valuetest.cpp @@ -591,3 +591,15 @@ TEST(Value, RemoveLastElement) { objVal.RemoveMember("var3"); // Assertion here in r61 EXPECT_FALSE(objVal.HasMember("var3")); } + +// Issue 38: Segmentation fault with CrtAllocator +TEST(Document, CrtAllocator) { + typedef GenericValue, CrtAllocator> V; + + V::AllocatorType allocator; + V o(kObjectType); + o.AddMember("x", 1, allocator); // Should not call destructor on uninitialized name/value of newly allocated members. + + V a(kArrayType); + a.PushBack(1, allocator); // Should not call destructor on uninitialized Value of newly allocated elements. +}