Cached properties causing exception upon deserializer reuse


In version 2.20:

If I create a SharpSerializer object, then call SharpSerializer.Deserialize(...) multiple times using the same object, an exception is thrown. I tracked it down to the private field Dictionary<,> XmlPropertyDeserializer._propertyCache. That dictionary is never cleared, but upon reuse (e.g. attempting to reload the same file), a duplicate key is added to _propertyCache which raises an exception. It appears that it only happens when loading Complex type properties and not Simple properties.

At about line 144 in XMLPropertyDeserializer.cs:
                _propertyCache.Add(referenceId, referenceTarget);
To duplicate the problem, serialize a class with a "complex" property (i.e. class). Deserialize it multiple times using the same SharpSerializer object.


SemCharlie wrote Jun 18, 2014 at 10:48 PM


Its surely too much of a coincidence that we have the same issue for the BinaryPropertySerializer?

I added simple guard around the Add() operation, it only Adds it if it's not present already... seems to work for my limited scenario - feel free to try.

Good Luck!

keyboarder wrote Jun 19, 2014 at 1:00 PM

SemCharlie, Thanks for pointing that out. I quickly read through other issue headings and missed that somehow, otherwise I would have just added a comment rather than a new issue. Perhaps someone can delete/merge this with the original post.

I will add additional comments to the other post linked to by SemCharlie.

keyboarder wrote Jun 19, 2014 at 1:19 PM

Okay, contrary to my other post I'm commenting here again to respond to SemCharlie's suggestion to "guard around the Add() operation." I can see how that would eliminate an exception in the most basic scenario. But consider re-using the deserializer object on another data stream, one with matching referenceId values (i.e. dictionary key values), but otherwise a data stream with a completely different scheme. If the code simply "guards" the Add() operation, but leaves existing values, then that dictionary could be full of inapplicable properties and it would exclude other necessary properties.

Instead, I cleared the dictionary in the entry method
   public Property Deserialize()
This also works to eliminate the exception, but should fix the problem in the most general case. I do not see a need to save the cached values beyond the current call to Deserialize().

professor_k wrote Mar 19 at 12:03 AM

Or you can just recreate serializer object before every use, it's not heavy operation. What's good, it works with nuget package version without need to recompile and host own feed.

Anyway, code fix seem to be trivial. What about the maintainer, anyone seen polo recently?