Skip to content

Commit

Permalink
Merge pull request #1001 from nlohmann/leak
Browse files Browse the repository at this point in the history
Fix memory leak during parser callback
  • Loading branch information
nlohmann authored Mar 9, 2018
2 parents cf60e18 + aa8fc2a commit e737de8
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
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));
}

/// 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

0 comments on commit e737de8

Please sign in to comment.