Question

I am developing a game using cocos2d-x and C++, and I need to load a bunch of YAML files for this application. I tried using the yaml-cpp library with quite good results.

The problem is that this library seems to be very unstable (at least under cocos2d-x on iOS), since almost 20% of the time it fails loading the same YAML file throwing "end of map not found", "invalid map element", or errors like these ones.

I followed the HowToParseADocument guide, so I think I got it correct. But, since it's not 100% reliable, I am looking for something more stable. Eg:

long size = 0;
unsigned char *yaml = FileUtils::getInstance()->getFileData("file.yml", "r", &size);
std::stringstream is;
is << yaml;

YAML::Parser parser(is);
YAML::Node doc;
while(parser.GetNextDocument(doc)) {
  instance->settings = doc.Clone();
}

The parser usally breaks at the parser.GetNextDocument(doc) call. The document I am trying to read is plain YAML with key: value lists in this simple form:

# Comment
section1:
  param1: value1
  param2: value2

# Comment
section2:
  param1: value1
  param2: value2

Edit

I am not allowed to disclose the content of the original YAML file, but I can give you some information:

  1. It only contains maps, and not arrays, aliases or other particular constructs
  2. Those values are integers, float or strings
  3. It has been linted with this free tool, with success.

The code I used to read it, posted up there, it's always in that form, and I do not modify it to make the app run correctly. It's just that the app starts and works or starts and does not work. Since I am changing nothing in the middle, I really do not understand what's happening.

Was it helpful?

Solution

It's a bit hard to guess at the solution because you won't provide an actual example, but:

Who owns the data at the unsigned char* returned by getFileData? If that function itself owns the data, then it is no longer valid after the function returns, and so all sorts of crazy stuff might happen.

To validate what's happening here (beyond looking at the implementation of getFileData), you could print out is.string() before calling YAML::Parser parser(is); and see if that prints the expected YAML.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top