#1 2022-11-02 08:29:41

Chaa
Member
Registered: 2011-03-26
Posts: 245

Problem with TOrm.CreateCopy

CreateCopy copies DocVariant fields by reference, because GetVariantProp called with byref=true, and SetVariantProp calls VarCopyDeep, but VarCopyDeep cannot find custom variant in FindCustomVariantType with type varVariantByRef, and call oleaut32.VariantCopy.

Next, when original object destroyed, reference becomes invalid, and bad things happens.

I my case, TJsonWriter.WriteObject fails when I pass ORM object with DocVariant field, that was previously copied via CreateCopy, and original object was destroyed.

Let's say I have object like this:

TOrmA = class(TOrm)
  FOpts: Variant;
published
  Opts: Variant read FOpts write FOpts;
end;

Then I load object from DB or create a new one, and call CreateCopy:

a := TOrmA.Create;
a.Opts := _Obj([]);
b := a.CreateCopy;
a.Free;

Next call to TJsonWriter.WriteObject fails with EJsonException with message 'TJsonWriter.AddVariant VType=<random number>'.

Offline

#2 2022-11-02 18:39:36

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,359
Website

Re: Problem with TOrm.CreateCopy

Offline

#3 2022-11-03 05:00:25

Chaa
Member
Registered: 2011-03-26
Posts: 245

Re: Problem with TOrm.CreateCopy

Yes, It helps.

But I think the root case is in the TRttiProp.SetVariantProp - it does not handle varVariantByRef.
But varByRef used in many parts of the core units.

For example, TOrm.SetFieldVariant may also be affected:

 v := _Json('{arr:[1,2]}');
 a.SetFieldVariant('Opts', v.arr); // v.arr will be returned by reference

Offline

#4 2022-11-08 18:50:04

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,359
Website

Re: Problem with TOrm.CreateCopy

Isn't it fixed no?

Offline

#5 2022-11-09 09:24:01

Chaa
Member
Registered: 2011-03-26
Posts: 245

Re: Problem with TOrm.CreateCopy

ab wrote:

Isn't it fixed?

I check CreateCopy and SetFieldVariant. Now it works OK.

Thanks to you and commit https://github.com/synopse/mORMot2/comm … e371698401 !

Offline

Board footer

Powered by FluxBB