fix #109: use weak_ptr in schema_refs

This commit is contained in:
Patrick Boettcher 2020-05-01 11:18:47 +02:00
parent 2cc7e9aaa5
commit a0fca479f6

View File

@ -62,22 +62,26 @@ public:
class schema_ref : public schema class schema_ref : public schema
{ {
const std::string id_; const std::string id_;
std::shared_ptr<schema> target_; std::weak_ptr<schema> target_;
void validate(const json::json_pointer &ptr, const json &instance, json_patch &patch, error_handler &e) const final void validate(const json::json_pointer &ptr, const json &instance, json_patch &patch, error_handler &e) const final
{ {
if (target_) auto target = target_.lock();
target_->validate(ptr, instance, patch, e);
if (target)
target->validate(ptr, instance, patch, e);
else else
e.error(ptr, instance, "unresolved schema-reference " + id_); e.error(ptr, instance, "unresolved or freed schema-reference " + id_);
} }
const json &defaultValue(const json::json_pointer &ptr, const json &instance, error_handler &e) const override const json &defaultValue(const json::json_pointer &ptr, const json &instance, error_handler &e) const override
{ {
if (target_) auto target = target_.lock();
return target_->defaultValue(ptr, instance, e);
if (target)
return target->defaultValue(ptr, instance, e);
else else
e.error(ptr, instance, "unresolved schema-reference " + id_); e.error(ptr, instance, "unresolved or freed schema-reference " + id_);
return EmptyDefault; return EmptyDefault;
} }
@ -87,7 +91,7 @@ public:
: schema(root), id_(id) {} : schema(root), id_(id) {}
const std::string &id() const { return id_; } const std::string &id() const { return id_; }
void set_target(std::shared_ptr<schema> target) { target_ = target; } void set_target(const std::shared_ptr<schema> &target) { target_ = target; }
}; };
} // namespace } // namespace