From 4ee17e67b1a4198f3afa8e2df4624acd5c7aac1f Mon Sep 17 00:00:00 2001 From: "miloyip@gmail.com" Date: Wed, 14 Nov 2012 03:33:10 +0000 Subject: [PATCH] Fixed Issue 38: Segmentation fault with CrtAllocator git-svn-id: https://rapidjson.googlecode.com/svn/trunk@80 c5894555-1306-4e8d-425f-1f6f381ee07c --- include/rapidjson/document.h | 6 +++--- test/unittest/valuetest.cpp | 12 ++++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index afe5354..7e90899 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 1578c03..60222a9 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. +}