Is That Pain Normal?
My Kingdom for an OpenOffice Writer Template!

Using object_copy() on Objects with Parent References

Important safety tip - the object_copy() method in Vera does a deep copy of everything, which causes problems if the object you're trying to copy has back references to parent objects (thus potentially causing infinite recursion or severe performance degradation).

One solution to this problem is to nullify the back references before doing the object copy.  Here's how I did it on a recent project:

class MyBase {
    virtual task setParent(MyBase parent);
    virtual function getParent();
    virtual function MyBase copy();
}

function MyBase MyBase::copy() {
  MyBase     parent   = this.getParent();

  // Clear parent so we don't get stuck in
  // an infinite loop during object_copy();
  this.setParent(null);

  copy = this.object_copy();

  this.setParent(parent);
  copy.setParent(parent);

  this.setParentModule(pMod);
  copy.setParentModule(pMod);

}

The nice thing about this solution is that I don't need to know whether any fields were added in any specializations (one caveat - if you add more back-references in specializations you'll have to rewrite the copy function to nullify the new pointers as well).  I also don't have to write my own clone() method.

Comments