I'm having a similar issue to this fellow, but the problem is not the same cause as in that answer.
I'm getting a const char *
from a C function. This function is actually implemented in C++ and it returns a struct by value which contains the return value of std::string c_str()
which is a C pointer to the string contents. This string lives in static memory. Since the pointer is copied into the return, I would expect the data to be fine.
According to Xcode's debugger, the string I get back is actually the correct length as I'd expect (see countAndFlags
below with correct character count of 7), but the contents are garbage. This is in a UITableView method, and what's very strange is that the garbage problem disappears if I use the reloadData()
TableView method (which I've wired to a button) which then has valid content for the name
variable below. However this subsequent invocation of reloadData()
does not alter any of the data used elsewhere in the table, only this problematic string garbage goes away.
Here is the debugger on first pass, showing garbage but correct length:
And here is the next pass when the text is correct:
I'm at a loss to understand this behaviour. The struct s
contains others properties that are not corrupted when they are returned, but this char *
is garbage on first pass only, with correct length nonetheless.
I'd appreciate any pointers (no pun intended) on what is happening here.
Finally, here is the C++ code for the function returning the C string:
scene getScene(sceneID s){
static std::map<sceneID, std::vector<nodeID> > nodeArrays;
auto ss = globalObjects.scenes.at(s);
auto sceneroots = ss.roots;
if (nodeArrays.count(s)>0) nodeArrays.at(s).clear();
else confirmInsertion(nodeArrays.emplace(s, std::vector<nodeID>{}));
auto v = nodeArrays.at(s);
v.reserve(sceneroots.size());
for (const auto&i:sceneroots) v.push_back(i);
scene sr;
sr.name=ss.name.c_str();
sr.roots=vec2array(v);
return sr;
}
If I log sr.name
it is always correct.
Here is scene
:
typedef struct {
const char* name;
arrayID roots;
} scene;
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…