Should return std::string

This is only used in C++, so there is no need to g_strdup in here. Also,
this fixes a memory leak in KvpFrameImpl.
This commit is contained in:
lmat 2017-08-18 17:44:53 -04:00
parent 048b8e37a1
commit a9cc488cbf
3 changed files with 5 additions and 13 deletions

View File

@ -35,7 +35,7 @@ test_kvp_get_slot (int run,
{ {
gchar* tmp; gchar* tmp;
failure_args (msg, __FILE__, __LINE__, "run=%d", run); failure_args (msg, __FILE__, __LINE__, "run=%d", run);
printf (" Value is %s\n", test_val2->to_string ()); printf (" Value is %s\n", test_val2->to_string ().c_str ());
} }
} }

View File

@ -149,16 +149,8 @@ struct to_string_visitor : boost::static_visitor<void>
/*Since val is passed by value, we can modify it*/ /*Since val is passed by value, we can modify it*/
for (;val; val = val->next) for (;val; val = val->next)
{ {
gchar *tmp3;
auto realvalue = static_cast<const KvpValue *>(val->data); auto realvalue = static_cast<const KvpValue *>(val->data);
tmp3 = realvalue->to_string(); output << ' ' << realvalue->to_string() << ',';
output << ' ';
if (tmp3)
{
output << tmp3;
g_free(tmp3);
}
output << ',';
} }
output << " ]"; output << " ]";
@ -207,7 +199,7 @@ struct to_string_visitor : boost::static_visitor<void>
} }
}; };
char * std::string
KvpValueImpl::to_string() const noexcept KvpValueImpl::to_string() const noexcept
{ {
std::ostringstream ret; std::ostringstream ret;
@ -215,7 +207,7 @@ KvpValueImpl::to_string() const noexcept
boost::apply_visitor(visitor, datastore); boost::apply_visitor(visitor, datastore);
/*We still use g_strdup since the return value will be freed by g_free*/ /*We still use g_strdup since the return value will be freed by g_free*/
return g_strdup(ret.str().c_str()); return ret.str();
} }
static int static int

View File

@ -137,7 +137,7 @@ struct KvpValueImpl
KvpValueImpl::Type get_type() const noexcept; KvpValueImpl::Type get_type() const noexcept;
char * to_string() const noexcept; std::string to_string() const noexcept;
template <typename T> template <typename T>
T get() const noexcept; T get() const noexcept;