Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unintentional free of property in Neon.Core.Persistence.JSON #35

Closed
dualarrow opened this issue Feb 14, 2021 · 1 comment
Closed

Unintentional free of property in Neon.Core.Persistence.JSON #35

dualarrow opened this issue Feb 14, 2021 · 1 comment

Comments

@dualarrow
Copy link

dualarrow commented Feb 14, 2021

I've been having a problem with deserializing a class I have. It seems to destroy the property when it should not.

The code is in line 1247 (method TNeonDeserializerJSON.ReadMembers) and the 2 lines are

    LMemberValue := ReadDataMember(LParam, LNeonMember.GetValue);
    LNeonMember.SetValue(LMemberValue);

if I have a class with a property like

property MyProp: TObject read GetMyProp write SetMyProp;

and the SetMyProp looks like this

Procedure SomeObject.SetMyProp(Value: TObject);
begin
if Assigned(FMyProp) then
FMyProp.Free;
FMyProp := Value;
end;

then the 2 mentioned lines perform as follows.

The 1st line gets a copy of the object into LMemberValue
The second line then tries to set the value to the same value, but this ends up disposing of the value.

I suspect this is possibly a technicalality of the code and may not really be a bug, but might be something that cannot easily be remedied. Perhaps a new attribute could solve it. In the mean time I'll try to disable my free in the property setter until after the deserializing has completed.

Another solution would be not to free it in the setter but have the caller free it before setting it, but that doesn't quite feel right.

Any thoughts ?

@dualarrow
Copy link
Author

I found a solution. I changed my setter to be

Procedure SomeObject.SetMyProp(Value: TObject);
begin
if Assigned(FMyProp) and (pointer(Value) <> pointer(FMyProp)) then
FMyProp.Free;
FMyProp := Value;
end;

so it now wont free it if its the same instance. I'll close this but leave the comments here in case they help someone else.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant