Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory leak during parser callback #1001

Merged
merged 2 commits into from
Mar 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/nlohmann/detail/input/lexer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1130,7 +1130,7 @@ class lexer
}

/// return current string value (implicitly resets the token; useful only once)
std::string move_string()
std::string&& move_string()
{
return std::move(token_buffer);
}
Expand Down
1 change: 1 addition & 0 deletions include/nlohmann/detail/input/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ class parser

if (keep and callback and not callback(depth, parse_event_t::value, result))
{
result.m_value.destroy(result.m_type);
result.m_type = value_t::discarded;
}
}
Expand Down
6 changes: 3 additions & 3 deletions include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,7 @@ class basic_json
/// constructor for rvalue strings
json_value(string_t&& value)
{
string = create<string_t>(std::move(value));
string = create<string_t>(std::forward < string_t&& > (value));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::move was correct here (and in all the other places in this PR) since value is not a forwarding reference, but an rvalue reference.

The behaviour is the same in this case, but is quite confusing.

}

/// constructor for objects
Expand All @@ -965,7 +965,7 @@ class basic_json
/// constructor for rvalue objects
json_value(object_t&& value)
{
object = create<object_t>(std::move(value));
object = create<object_t>(std::forward < object_t&& > (value));
}

/// constructor for arrays
Expand All @@ -977,7 +977,7 @@ class basic_json
/// constructor for rvalue arrays
json_value(array_t&& value)
{
array = create<array_t>(std::move(value));
array = create<array_t>(std::forward < array_t&& > (value));
}

void destroy(value_t t) noexcept
Expand Down
9 changes: 5 additions & 4 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2969,7 +2969,7 @@ class lexer
}

/// return current string value (implicitly resets the token; useful only once)
std::string move_string()
std::string&& move_string()
{
return std::move(token_buffer);
}
Expand Down Expand Up @@ -3526,6 +3526,7 @@ class parser

if (keep and callback and not callback(depth, parse_event_t::value, result))
{
result.m_value.destroy(result.m_type);
result.m_type = value_t::discarded;
}
}
Expand Down Expand Up @@ -10561,7 +10562,7 @@ class basic_json
/// constructor for rvalue strings
json_value(string_t&& value)
{
string = create<string_t>(std::move(value));
string = create<string_t>(std::forward < string_t&& > (value));
}

/// constructor for objects
Expand All @@ -10573,7 +10574,7 @@ class basic_json
/// constructor for rvalue objects
json_value(object_t&& value)
{
object = create<object_t>(std::move(value));
object = create<object_t>(std::forward < object_t&& > (value));
}

/// constructor for arrays
Expand All @@ -10585,7 +10586,7 @@ class basic_json
/// constructor for rvalue arrays
json_value(array_t&& value)
{
array = create<array_t>(std::move(value));
array = create<array_t>(std::forward < array_t&& > (value));
}

void destroy(value_t t) noexcept
Expand Down