Salome HOME
Fix of crash on feature in new part creation
[modules/shaper.git] / src / Model / Model_AttributeReference.cpp
index 91de64eb567de9f94889a6274c98f8ae8c6dcebc..9c51e545b66ee0983cfcb17eb8747ce7dfe96654 100644 (file)
@@ -21,9 +21,12 @@ void Model_AttributeReference::setValue(ObjectPtr theObject)
 {
   if(!theObject)
     return;
-  if (!myIsInitialized || value() != theObject) {
+  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());
+      theObject->data());
     TDF_Label anObjLab = aData->label().Father(); // object label
 
     if (owner()->document() == theObject->document()) { // same document, use reference attribute
@@ -43,10 +46,7 @@ void Model_AttributeReference::setValue(ObjectPtr theObject)
     }
     // do it before the transaction finish to make just created/removed objects know dependencies
     // and reference from composite feature is removed automatically
-    FeaturePtr anOwnerFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner());
-    if (anOwnerFeature.get()) {
-      aData->addBackReference(anOwnerFeature, id());
-    }
+    ADD_BACK_REF(theObject);
 
     owner()->data()->sendAttributeUpdated(this);
   }