Salome HOME
Fix of crash on feature in new part creation
[modules/shaper.git] / src / Model / Model_AttributeReference.cpp
index 2b0e15756ff84c9766ac0226447aab13a9ad63c5..9c51e545b66ee0983cfcb17eb8747ce7dfe96654 100644 (file)
@@ -21,10 +21,13 @@ void Model_AttributeReference::setValue(ObjectPtr theObject)
 {
   if(!theObject)
     return;
-  if (!myIsInitialized || value() != theObject) {
-      std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(
-          theObject->data());
-      TDF_Label anObjLab = aData->label().Father(); // object label
+  ObjectPtr aValue = value();
+  if (!myIsInitialized || aValue != theObject) {
+    REMOVE_BACK_REF(aValue);
+
+    std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(
+      theObject->data());
+    TDF_Label anObjLab = aData->label().Father(); // object label
 
     if (owner()->document() == theObject->document()) { // same document, use reference attribute
 
@@ -41,6 +44,9 @@ void Model_AttributeReference::setValue(ObjectPtr theObject)
       TDF_Tool::Entry(anObjLab, anEntry);
       TDataStd_AsciiString::Set(myRef->Label(), anEntry);
     }
+    // do it before the transaction finish to make just created/removed objects know dependencies
+    // and reference from composite feature is removed automatically
+    ADD_BACK_REF(theObject);
 
     owner()->data()->sendAttributeUpdated(this);
   }
@@ -69,7 +75,9 @@ ObjectPtr Model_AttributeReference::value()
           owner()->document());
       if (aDoc) {
         TDF_Label aRefLab = myRef->Get();
-        return aDoc->object(aRefLab);
+        if (!aRefLab.IsNull()) {  // it may happen with old document, issue #285
+          return aDoc->object(aRefLab);
+        }
       }
     }
   }