Manipulating Lazy fields via Rtti generates access violation

58 views
Skip to first unread message

João Antônio Duarte

unread,
Jan 17, 2022, 5:59:15 PM1/17/22
to Spring4D
Hello everyone,
I'm working on a JSON serializer for my classes, and when trying to get values of properties of type Lazy<T> I get an access violation.
I have attached a code example that generates this error. I don't know if I'm doing something wrong or it really is a bug in Spring4D's Lazy type.

Note: I'm using Spring's develop branch.
JsonSerializer.zip

Stefan Glienke

unread,
Jan 18, 2022, 8:05:44 AM1/18/22
to Spring4D
There was a refcounting bug caused by TryGetLazyValue which resulted in premature destruction of the internal instance holding the lazy value.
I fixed that in the develop branch.

Also FWIW your TStream serialization is missing setting the Position to 0 before passing it to TNetEncoding.Base64.Encode.
Furthermore, you could add an additional check on TStringStream and directly pass its DataString to the TJSONString

João Duarte

unread,
Jan 18, 2022, 9:40:29 AM1/18/22
to Spring4D
Thank you very much!

Stefan Glienke

unread,
Jan 18, 2022, 10:36:14 AM1/18/22
to Spring4D
I also fixed another issue with reference counting and ownership which enables simply writing 

  FLazyValue1 := Lazy<TStringStream>.Create;
  FLazyValue2 := Lazy<TStringStream>.Create;

This works if the factory simply should call the default ctor of the class as is the case here. It also takes ownership of the instance (which it did not before the fix).

João Duarte

unread,
Jan 18, 2022, 12:04:52 PM1/18/22
to Spring4D
Wow, this is great! Thanks for the wonderful work!
Reply all
Reply to author
Forward
0 new messages