Commit 84d74052 authored by miloyip's avatar miloyip

Fix memory bugs

parent 0fb2b803
...@@ -1174,14 +1174,8 @@ public: ...@@ -1174,14 +1174,8 @@ public:
} }
~GenericSchemaDocument() { ~GenericSchemaDocument() {
while (!schemaMap_.Empty()) { while (!schemaMap_.Empty())
SchemaEntry* e = schemaMap_.template Pop<SchemaEntry>(1); schemaMap_.template Pop<SchemaEntry>(1)->~SchemaEntry();
if (e->owned) {
e->schema->~SchemaType();
Allocator::Free(e->schema);
e->~SchemaEntry();
}
}
RAPIDJSON_DELETE(ownAllocator_); RAPIDJSON_DELETE(ownAllocator_);
} }
...@@ -1198,43 +1192,48 @@ private: ...@@ -1198,43 +1192,48 @@ private:
struct SchemaEntry { struct SchemaEntry {
SchemaEntry(const PointerType& p, SchemaType* s, bool o) : pointer(p), schema(s), owned(o) {} SchemaEntry(const PointerType& p, SchemaType* s, bool o) : pointer(p), schema(s), owned(o) {}
~SchemaEntry() {
if (owned)
schema->~SchemaType();
}
PointerType pointer; PointerType pointer;
SchemaType* schema; SchemaType* schema;
bool owned; bool owned;
}; };
void CreateSchemaRecursive(const SchemaType** schema, const PointerType& pointer, const ValueType& v) { void CreateSchemaRecursive(const SchemaType** schema, const PointerType& pointer, const ValueType& v) {
*schema = SchemaType::GetTypeless(); if (schema)
*schema = SchemaType::GetTypeless();
if (v.GetType() == kObjectType) { if (v.GetType() == kObjectType) {
const SchemaType* s = GetSchema(pointer); const SchemaType* s = GetSchema(pointer);
if (!s) if (!s)
CreateSchema(schema, pointer, v); CreateSchema(schema, pointer, v);
else else if (schema)
*schema = s; *schema = s;
for (typename ValueType::ConstMemberIterator itr = v.MemberBegin(); itr != v.MemberEnd(); ++itr) for (typename ValueType::ConstMemberIterator itr = v.MemberBegin(); itr != v.MemberEnd(); ++itr)
CreateSchemaRecursive(&s, pointer.Append(itr->name), itr->value); CreateSchemaRecursive(0, pointer.Append(itr->name), itr->value);
} }
else if (v.GetType() == kArrayType) { else if (v.GetType() == kArrayType)
const SchemaType* s;
for (SizeType i = 0; i < v.Size(); i++) for (SizeType i = 0; i < v.Size(); i++)
CreateSchemaRecursive(&s, pointer.Append(i), v[i]); CreateSchemaRecursive(0, pointer.Append(i), v[i]);
}
} }
void CreateSchema(const SchemaType** schema, const PointerType& pointer, const ValueType& v) { void CreateSchema(const SchemaType** schema, const PointerType& pointer, const ValueType& v) {
RAPIDJSON_ASSERT(pointer.IsValid()); RAPIDJSON_ASSERT(pointer.IsValid());
if (v.IsObject()) { if (v.IsObject()) {
if (!HandleRefSchema(pointer, schema, v)) { if (!schema || !HandleRefSchema(pointer, schema, v)) {
SchemaType* s = new (allocator_->Malloc(sizeof(SchemaType))) SchemaType(this, allocator_, pointer, v); SchemaType* s = new (allocator_->Malloc(sizeof(SchemaType))) SchemaType(this, allocator_, pointer, v);
new (schemaMap_.template Push<SchemaEntry>()) SchemaEntry(pointer, s, true); new (schemaMap_.template Push<SchemaEntry>()) SchemaEntry(pointer, s, true);
*schema = s; if (schema)
*schema = s;
} }
} }
} }
bool HandleRefSchema(const Pointer& source, const SchemaType** schema, const ValueType& v) { bool HandleRefSchema(const Pointer& source, const SchemaType** schema, const ValueType& v) {
RAPIDJSON_ASSERT(schema != 0);
typename ValueType::ConstMemberIterator itr = v.FindMember("$ref"); typename ValueType::ConstMemberIterator itr = v.FindMember("$ref");
if (itr == v.MemberEnd()) if (itr == v.MemberEnd())
return false; return false;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment