Problem when calling toJSON()

anonymous via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jan 31 17:08:46 PST 2016


On 01.02.2016 01:29, Alex Herrmann wrote:
> This problem is solved! Sorry for not updating the question.
>
> It was actually a problem with me not iduping it, and the memory being
> reclaimed or something similar. I did also change to stdx.data.json
> (std_data_json on dub).

You don't seem to be clear on what exactly went wrong. So here's an 
explanation:

On 31.01.2016 23:52, Alex Herrmann wrote:
 >      auto outmd5 = JSONValue(cast(string) toHexString(md5_hash));

That cast is bad. You're casting a fixed-size char array to string, 
effectively slicing it. The data will get corrupted when the fixed-size 
array goes out of scope. Which happens immediately here, as you don't 
store the result of the toHexString call anywhere.

As you mentioned, .idup instead of cast is the proper way to do this. 
std.conv.to!string is another option.

Generally, casts should be avoided. It's easy to mess up with them in 
subtle ways.


More information about the Digitalmars-d-learn mailing list