Public field goes null after overriding OnDraw in CustomMeshObject

So I’ve create this CustomMeshObject class and I would like it hold on to a Brep object:

public class cMesh : Rhino.DocObjects.Custom.CustomMeshObject
    {
        public RhinoDoc doc;
        public Transform xform;
        public GeometryBase attachedGeo;

        public cMesh()
        {

        }

        public cMesh(Rhino.Geometry.Mesh mesh)
        {
            this.SetMesh(mesh);
        }

        public cMesh(Rhino.Geometry.Brep brep)
        {
            Mesh tmpMesh = new Mesh();
            if (brep != null)           
                tmpMesh = Mesh.CreateFromBox(new Box(brep.GetBoundingBox(true)), 1, 1, 1);
            
            this.SetMesh(tmpMesh);
            this.attachedGeo = brep.DuplicateBrep();
        }

        public void Update()
        {
            doc.Objects.Transform(this.Id, xform, true);
            attachedGeo.Transform(xform);
        }

        protected override void OnDraw(Rhino.Display.DrawEventArgs e)
        {
            var geo = this.attachedGeo; //without this line, attachedGeo is fine!

            base.OnDraw(e);
        }

If I don’t override OnDraw, my class maintains the reference to attachedGeo. However, if I do override OnDraw (and use attachedGeo in any way, attachedGeo gets set to null on the next update…

Seems strange.

I think you should also override OnDuplicate (or a similarly named method). Then in that method keep a reference to your member variable.

Thanks, that worked. The key was to cast the Source as my custom mesh type and then pass the references accordingly.