             parentNode.parentNode = xmlDocument;
         }
         
-        encodeValue(obj, qname, parentNode, true, false, true);
+        encodeValue(obj, qname, parentNode, true, referenceOnly, true);
+        
+        // dump the recursionMap at the end
+        recursionMap = null;
         
         return xmlDocument;
     }

        }
        else if (isRoot)
        {
            return internal_encodeValue(obj, qname, parentNode);
        }
        else if (referenceOnly)
        {

            //TODO: synthesize the attrList structure
            
        }
        else if (("id" in obj) && obj["id"]) // special case id property in non __attributes case
        {
            attrList.push({propname:"id"});
        }
