Well, not quite. Let me try to rephrase my reply.
The "CopyOf" method creates a copy, as well as an assignment to the "asResult" property, because of which the two lines below basically do the same thing, just in a different way:
Result.asObject := pItem.copyOf; // this assigns a copy of "pItem" to Result
Result.asRecord := TRtcRecord(pItem); // this ALSO assigns a copy of "pItem" to Result
If you want to assign the original "pItem" object and NOT a copy of that object (in which case the Result object would manage your object, so you should NOT destroy it yourself), you have to assign your object to the "asObject" property, like this:
What you should NOT do, is assigning a "CopyOf" to the "asRecord" property, because that will create two copies, leaking one of them. That is what I was trying to point out in my original reply above. In other words,
this line is WRONG, because it will result in a
memory leak:
Result.asRecord := TRtcRecord(pItem.copyOf); // this assigns a copy of a copy of "pItem" to Result, "leaking" the 1st copy
Best Regards,
Danijel Tkaclec