Share via


Issues with Cloning MarshalByRefObject

Cloning MarshalByRefObjects can lead to some unexpected behaviour. When a MBR object is marshalled, it gets an unique identity which contains a unique URI to identify this object in the appdomain. When an external call is made to this object it is identified by this URI. An identity once created doesnt change each time the object is marshalled. Now if an MBR object with its identity set is cloned using Object.MemberwiseClone(since this deep clones all private/public fields as well) the cloned object would get the same identity. If the clone is marshalled out, wouldnt get a new identity since it already has one. Thus if calls are made to the cloned object, the invocation could actually be done on the original object, which could lead to some really unwanted results. So please be extra careful while cloning MBRs when the cloned object could potentially be marshalled as well.

In v2.0 of the framework, MBR probably would have a clone helper which would null out the identity after cloning.

Comments

  • Anonymous
    November 24, 2004
    Doesn't the MarshalByRef also allow you to cross multiple app domains, so in theory could you maybe clone to a new app domain?

    Just more curious for my own knowledge I use MarshalByRef all the time for anything in Dynamic loading of assemblies but MemberwiseClone never found an interesting use for it.
  • Anonymous
    November 24, 2004

    MarshalByRefObjects are never cloned (or serialized) across appdomains. They are passed by reference. This particular issue (cloning the identity) would only occur if MemberwiseClone is used.

    Thanks