teris / rapidjson

Automatically exported from code.google.com/p/rapidjson
MIT License
0 stars 0 forks source link

Blaireaux #116

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1.    string str = "2";
      const char *c = str.c_str();
       changedStatus.AddMember((char*)c, turned_on ? "on" : "off",    this->_doc->GetAllocator());
2. Received : "changed_statuses" => {"\xB8" =? "off"}

What is the expected output? What do you see instead?
Received : "changed_statuses" => {"2" =? "off"}

What version of the product are you using? On what operating system?
-

Please provide any additional information below.
-

Blaireaux.

Original issue reported on code.google.com by Fannie.P...@gmail.com on 1 Nov 2014 at 8:04

GoogleCodeExporter commented 8 years ago
This is a user error.  "str" (and therefore "*c") is quite likely destroyed, 
when you try to read the object again.  You need to copy the string, if you 
can't guarantee that the contents' lifetime is sufficiently long.

Try to change your code to:
  string str = "2";
  Value name(str.c_str(), this->_doc->GetAllocator());
  changedStatus.AddMember(name, turned_on ? "on" : "off",    this->_doc->GetAllocator());
Alternatively, if you really have a constant string "2" here, then you can 
safely use:

  Value name("2"); // no copy (but constant string literal)
  changedstatus.AddMember(name, ...); 

hth,
Philipp

Original comment by philipp....@gmail.com on 2 Nov 2014 at 7:17