fixed some memory leaks in Space, pointers in tables and objects were not correctly 'deleted', affected methods:

set_empty_table()
set_empty_object()
add(const wchar_t * field, Space && space)
copy_value_object(const Value & value_from)
copy_value_table(const Value & value_from)
initialize_value_object_if_needed(ObjectType && obj)
initialize_value_table_if_needed(TableType && tab)
add_generic(const wchar_t * field, const ArgType & val)
htmlparserlistener
Tomasz Sowa 2 years ago
parent 4a1630b1ea
commit 009e240a8d

@ -278,7 +278,7 @@ void Space::set_empty_table()
{
if( type == type_table )
{
value.value_table.clear();
remove_value_table(true);
}
else
{
@ -290,7 +290,7 @@ void Space::set_empty_object()
{
if( type == type_object )
{
value.value_object.clear();
remove_value_object(true);
}
else
{
@ -629,9 +629,16 @@ Space & Space::add(const wchar_t * field, const Space * space)
Space & Space::add(const wchar_t * field, Space && space)
{
initialize_value_object_if_needed();
auto insert_res = value.value_object.insert(std::make_pair(field, nullptr));
insert_res.first->second = new Space(std::move(space));
if( insert_res.second )
{
insert_res.first->second = new Space(std::move(space));
}
else
{
insert_res.first->second->set(std::move(space));
}
return *(insert_res.first->second);
}
@ -2082,8 +2089,7 @@ bool Space::has_value(const wchar_t * field, const std::wstring & val) const
void Space::copy_value_from(const Space & space)
void Space::copy_from(const Space & space)
{
switch(space.type)
{
@ -2138,19 +2144,18 @@ void Space::copy_value_from(const Space & space)
void Space::copy_from(const Space & space)
{
copy_value_from(space);
}
void Space::copy_value_object(const Value & value_from)
{
initialize_value_object_if_needed();
value.value_object.clear();
if( type == type_object )
{
remove_value_object(true);
}
else
{
initialize_value_object_if_needed();
}
for(auto map_item : value_from.value_object)
{
@ -2162,8 +2167,14 @@ void Space::copy_value_object(const Value & value_from)
void Space::copy_value_table(const Value & value_from)
{
initialize_value_table_if_needed();
value.value_table.clear();
if( type == type_table )
{
remove_value_table(true);
}
else
{
initialize_value_table_if_needed();
}
for(Space * space : value_from.value_table)
{
@ -2194,7 +2205,7 @@ void Space::move_value_from(Space && space)
break;
default:
copy_value_from(space);
copy_from(space);
break;
}
}
@ -2351,6 +2362,7 @@ void Space::initialize_value_object_if_needed(ObjectType && obj)
}
else
{
remove_value_object(true);
value.value_object = std::move(obj);
}
}
@ -2377,6 +2389,7 @@ void Space::initialize_value_table_if_needed(TableType && tab)
}
else
{
remove_value_table(true);
value.value_table = std::move(tab);
}
}
@ -2438,7 +2451,7 @@ void Space::remove_value_wstring()
}
void Space::remove_value_object()
void Space::remove_value_object(bool only_clear)
{
for(auto map_item : value.value_object)
{
@ -2446,12 +2459,19 @@ void Space::remove_value_object()
map_item.second = nullptr;
}
value.value_object.~ObjectType();
type = type_null;
if( only_clear )
{
value.value_object.clear();
}
else
{
value.value_object.~ObjectType();
type = type_null;
}
}
void Space::remove_value_table()
void Space::remove_value_table(bool only_clear)
{
for(size_t i = 0 ; i < value.value_table.size() ; ++i)
{
@ -2459,8 +2479,15 @@ void Space::remove_value_table()
value.value_table[i] = nullptr;
}
value.value_table.~TableType();
type = type_null;
if( only_clear )
{
value.value_table.clear();
}
else
{
value.value_table.~TableType();
type = type_null;
}
}

@ -647,9 +647,16 @@ protected:
Space & add_generic(const wchar_t * field, const ArgType & val)
{
initialize_value_object_if_needed();
auto insert_res = value.value_object.insert(std::make_pair(field, nullptr));
insert_res.first->second = new Space(val);
if( insert_res.second )
{
insert_res.first->second = new Space(val);
}
else
{
insert_res.first->second->set(val);
}
return *(insert_res.first->second);
}
@ -1341,7 +1348,6 @@ protected:
}
void copy_value_from(const Space & space);
void copy_from(const Space & space);
void copy_value_object(const Value & value_from);
@ -1369,8 +1375,8 @@ protected:
void remove_value();
void remove_value_string();
void remove_value_wstring();
void remove_value_object();
void remove_value_table();
void remove_value_object(bool only_clear = false);
void remove_value_table(bool only_clear = false);
};

Loading…
Cancel
Save