Skip to content

Commit e737de8

Browse files
authored
Merge pull request nlohmann#1001 from nlohmann/leak
Fix memory leak during parser callback
2 parents cf60e18 + aa8fc2a commit e737de8

File tree

4 files changed

+10
-8
lines changed

4 files changed

+10
-8
lines changed

include/nlohmann/detail/input/lexer.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1130,7 +1130,7 @@ class lexer
11301130
}
11311131

11321132
/// return current string value (implicitly resets the token; useful only once)
1133-
std::string move_string()
1133+
std::string&& move_string()
11341134
{
11351135
return std::move(token_buffer);
11361136
}

include/nlohmann/detail/input/parser.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ class parser
403403

404404
if (keep and callback and not callback(depth, parse_event_t::value, result))
405405
{
406+
result.m_value.destroy(result.m_type);
406407
result.m_type = value_t::discarded;
407408
}
408409
}

include/nlohmann/json.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -953,7 +953,7 @@ class basic_json
953953
/// constructor for rvalue strings
954954
json_value(string_t&& value)
955955
{
956-
string = create<string_t>(std::move(value));
956+
string = create<string_t>(std::forward < string_t&& > (value));
957957
}
958958

959959
/// constructor for objects
@@ -965,7 +965,7 @@ class basic_json
965965
/// constructor for rvalue objects
966966
json_value(object_t&& value)
967967
{
968-
object = create<object_t>(std::move(value));
968+
object = create<object_t>(std::forward < object_t&& > (value));
969969
}
970970

971971
/// constructor for arrays
@@ -977,7 +977,7 @@ class basic_json
977977
/// constructor for rvalue arrays
978978
json_value(array_t&& value)
979979
{
980-
array = create<array_t>(std::move(value));
980+
array = create<array_t>(std::forward < array_t&& > (value));
981981
}
982982

983983
void destroy(value_t t) noexcept

single_include/nlohmann/json.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2969,7 +2969,7 @@ class lexer
29692969
}
29702970

29712971
/// return current string value (implicitly resets the token; useful only once)
2972-
std::string move_string()
2972+
std::string&& move_string()
29732973
{
29742974
return std::move(token_buffer);
29752975
}
@@ -3526,6 +3526,7 @@ class parser
35263526

35273527
if (keep and callback and not callback(depth, parse_event_t::value, result))
35283528
{
3529+
result.m_value.destroy(result.m_type);
35293530
result.m_type = value_t::discarded;
35303531
}
35313532
}
@@ -10561,7 +10562,7 @@ class basic_json
1056110562
/// constructor for rvalue strings
1056210563
json_value(string_t&& value)
1056310564
{
10564-
string = create<string_t>(std::move(value));
10565+
string = create<string_t>(std::forward < string_t&& > (value));
1056510566
}
1056610567

1056710568
/// constructor for objects
@@ -10573,7 +10574,7 @@ class basic_json
1057310574
/// constructor for rvalue objects
1057410575
json_value(object_t&& value)
1057510576
{
10576-
object = create<object_t>(std::move(value));
10577+
object = create<object_t>(std::forward < object_t&& > (value));
1057710578
}
1057810579

1057910580
/// constructor for arrays
@@ -10585,7 +10586,7 @@ class basic_json
1058510586
/// constructor for rvalue arrays
1058610587
json_value(array_t&& value)
1058710588
{
10588-
array = create<array_t>(std::move(value));
10589+
array = create<array_t>(std::forward < array_t&& > (value));
1058910590
}
1059010591

1059110592
void destroy(value_t t) noexcept

0 commit comments

Comments
 (0)