Reputation: 1334
I have this code:
JSONObject object;
if (value->IsObject())
{
object = value->AsObject();
const wchar_t *tmp = from_string(entity_id);
std::wcout << tmp << std::endl;
std::wcout.flush();
if (object.find(tmp) != object.end())
{
std::wcout << tmp << std::endl;
std::wcout.flush();
initFromJSON(object[tmp]->AsObject());
}
else
{
return false;
}
}
Problem here is that after inner if statement, the content of a tmp is empty. Before that it's not. When I try debuggin, it's everything ok, the content is not deleted. But when I run program, content is deleted. Any idea why?
Method from_string(entity_id)
only convert from string to wchar like this:
std::wstring result;
for (int i = 0; i<content.length(); i++)
result += wchar_t(content[i]);
return result.c_str();
Methods JSONObject::find(...)
and JSONObject::end()
are this:
iterator find(const key_type& __k) {return __tree_.find(__k);}
iterator end() _NOEXCEPT {return __tree_.end();}
I don't believe that is problem in find(...) or in end(). I'm guessing that problem is somewhere else but I cannot locate it. Because the content of wchar is empty after the if statement I cannot say
initFromJSON(object[tmp]->AsObject());
because object[tmp]
doesn't exist.
Any advice what am I doing wrong?
Upvotes: 0
Views: 86
Reputation: 35440
The issue is that your from_string
function returns a pointer to a local entity, namely the return value of std::wstring::c_str()
. Thus you've introduced undefined behavior.
I see no reason at all for the tmp
variable being a pointer. You should be able to do something like this instead:
std::wstring from_entity(...)
{
std::wstring result;
for (int i = 0; i<content.length(); i++)
result += wchar_t(content[i]);
return result;
}
And then this:
JSONObject object;
if (value->IsObject())
{
object = value->AsObject();
std::wstring tmp = from_string(entity_id);
std::wcout << tmp << std::endl;
std::wcout.flush();
if (object.find(tmp.c_str()) != object.end())
{
std::wcout << tmp << std::endl;
std::wcout.flush();
initFromJSON(object[tmp.c_str()]->AsObject());
}
else
{
return false;
}
}
Note that a std::wstring
is returned, not a pointer. Then within the function itself, you use c_str()
if you need to pass a const wchar_t*
.
Upvotes: 1