Skip to content
This repository has been archived by the owner on Feb 9, 2021. It is now read-only.

Commit

Permalink
Merge pull request #7 from valkenburg/fixLeak
Browse files Browse the repository at this point in the history
fix for memory leak when Internal is overwritten.
  • Loading branch information
nbsdx authored Sep 30, 2016
2 parents f909b24 + b3a4ba6 commit 8dd3e9b
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ class JSON
{ other.Type = Class::Null; other.Internal.Map = nullptr; }

JSON& operator=( JSON&& other ) {
ClearInternal();
Internal = other.Internal;
Type = other.Type;
other.Internal.Map = nullptr;
Expand Down Expand Up @@ -142,6 +143,7 @@ class JSON
}

JSON& operator=( const JSON &other ) {
ClearInternal();
switch( other.Type ) {
case Class::Object:
Internal.Map =
Expand Down Expand Up @@ -382,13 +384,8 @@ class JSON
if( type == Type )
return;

switch( Type ) {
case Class::Object: delete Internal.Map; break;
case Class::Array: delete Internal.List; break;
case Class::String: delete Internal.String; break;
default:;
}

ClearInternal();

switch( type ) {
case Class::Null: Internal.Map = nullptr; break;
case Class::Object: Internal.Map = new map<string,JSON>(); break;
Expand All @@ -402,6 +399,20 @@ class JSON
Type = type;
}

private:
/* beware: only call if YOU know that Internal is allocated. No checks performed here.
This function should be called in a constructed JSON just before you are going to
overwrite Internal...
*/
void ClearInternal() {
switch( Type ) {
case Class::Object: delete Internal.Map; break;
case Class::Array: delete Internal.List; break;
case Class::String: delete Internal.String; break;
default:;
}
}

private:

Class Type = Class::Null;
Expand Down

0 comments on commit 8dd3e9b

Please sign in to comment.