Closed biojppm closed 2 years ago
@aleos89 base64 cannot have anything to do with the code you pasted above; it takes explicit base64()
calls to have any base64 encoding/decoding. And I see nothing wrong with your sample as such.
Rather, I suspect you have a problem with memory lifetime issues.
(FWIW, this is the relevant quickstart code)
For example, when you do body["Id"] = c4::to_csubstr(str[0]);
, the node will not copy the memory; it will only be pointing at the memory in str[0]
. If str[0]
is destroyed and the ryml tree lives on, then the tree will be left pointing at the stale memory from str[0]
. For example:
ryml::Tree t;
auto body = t["body"]; // or something like this
{
std::string your = "your"; // valid only in this scope
std::string strings = "strings"; // valid only in this scope
char *the_strings[] = {&your[0], &strings[0], ....};
instance_readdb_sub(the_strings, columns, current);
}
std::cout << body["id"].val() << "\n"; // ERROR: stale memory
but this should be fine:
ryml::Tree t;
auto body = t["body"]; // or something like this
std::string your = "your";
std::string strings = "strings";
char *the_strings[] = {&your[0], &strings[0], ....};
instance_readdb_sub(the_strings, columns, current);
std::cout << body["id"].val() << "\n"; // OK! memory was not freed
Or alternatively you can use operator<<
to force copying the string to the tree's arena, at the cost of allocating and copying.
Let's address your other questions after this issue, as it is extremely important.
@biojppm That makes sense. The body
and tree
variables are declared globally in our case, so it should be behaving like your latter sample. The strings though that are parsed from the CSV file only live within that scope though, which is where instance_readdb_sub
is called.
I'll play around with <<
as the operator!
Note that operator=
will always be faster and less resource intensive than operator<<
. The downside is if you use =
you need to take care with the lifetime. Blue pill or red pill.
Gotcha! Well in our case again this converter is usually just ran whenever someone needs to convert their CSV to YAML. We have several CSV databases which we are slowly converting, so performance isn't a major issue here as this is a one-and-done situation.
Did this work for you?
Sorry for the delay. I've been following up on some bugs in our repo recently. I will leave a comment once I get back to testing this again here shortly!
@aleos89 closing this now. Feel free to reopen if you want to resume.
}