Repository: microsoft/terminal (language: cpp)

Issue:
a more effective implementation of Pane::LayoutSizeNode::operator=
https://github.com/microsoft/terminal/blob/fb597ed304ec6eef245405c9652e9b8a029b821f/src/cascadia/TerminalApp/Pane.LayoutSizeNode.cpp#L35-L46

Actually, we don't need `Pane::LayoutSizeNode::_AssignChildNode` to help define `Pane::LayoutSizeNode::operator=` . It makes things more complicated. Like LayoutSizeNode's copy constructor, LayoutSizeNode's copy assignment operator can be defined more effectively as follows:
```cpp
Pane::LayoutSizeNode& Pane::LayoutSizeNode::operator=(const LayoutSizeNode& other)
{
	size = other.size;
	isMinimumSize = other.isMinimumSize;

	firstChild = other.firstChild ? std::make_unique<LayoutSizeNode>(*other.firstChild) : nullptr;
	secondChild = other.secondChild ? std::make_unique<LayoutSizeNode>(*other.secondChild) : nullptr;
	nextFirstChild = other.nextFirstChild ? std::make_unique<LayoutSizeNode>(*other.nextFirstChild) : nullptr;
	nextSecondChild = other.nextSecondChild ? std::make_unique<LayoutSizeNode>(*other.nextSecondChild) : nullptr;

	return *this;
}
```



Discussion / hints:
So, if we did this, would we be able to just entirely get rid of `_AssignChildNode` (and then not even need to do #11963)? 
> So, if we did this, would we be able to just entirely get rid of `_AssignChildNode` (and then not even need to do #11963)?

@zadjii-msft  Yes! 

https://github.com/microsoft/terminal/blob/fb597ed304ec6eef245405c9652e9b8a029b821f/src/cascadia/TerminalApp/Pane.LayoutSizeNode.cpp#L71
`_AssignChildNode` is redundant and error-prone. Currently, at `_AssignChildNode` Line71, the memory leak has been occured because `nodeField.release();`  doesn't destroy the managed object and we lose the pointer to the managed object. The correct way is `nodeField.reset();` rather than `nodeField.release();` . See [unique_ptr::release - (cplusplus.com)](http://www.cplusplus.com/reference/memory/unique_ptr/release/) .
          
A more effective implementation of `Pane::LayoutSizeNode::operator=` is what I wrote above. If we did this,  we can delete the `_AssignChildNode` code block. 

There is a similar technique to define a class copy assignment operator in Google tensorflow source code: 
https://github.com/tensorflow/tensorflow/blob/3d30ef6f49285b9e08a7b3492a902c67678a94d7/tensorflow/compiler/xla/service/spmd/spmd_partitioner_util.cc#L731-L732
@zadjii-msft @DHowett 
Any update on this?
Sorry, been quite distracted the last few weeks. If you'd like to open a PR, feel free to! We'd love to take a look.

Fix patch:
diff --git a/src/cascadia/TerminalApp/Pane.LayoutSizeNode.cpp b/src/cascadia/TerminalApp/Pane.LayoutSizeNode.cpp
index f3cc8647ff1..75215b1fe63 100644
--- a/src/cascadia/TerminalApp/Pane.LayoutSizeNode.cpp
+++ b/src/cascadia/TerminalApp/Pane.LayoutSizeNode.cpp
@@ -37,37 +37,10 @@ Pane::LayoutSizeNode& Pane::LayoutSizeNode::operator=(const LayoutSizeNode& othe
     size = other.size;
     isMinimumSize = other.isMinimumSize;
 
-    _AssignChildNode(firstChild, other.firstChild.get());
-    _AssignChildNode(secondChild, other.secondChild.get());
-    _AssignChildNode(nextFirstChild, other.nextFirstChild.get());
-    _AssignChildNode(nextSecondChild, other.nextSecondChild.get());
+    firstChild = other.firstChild ? std::make_unique<LayoutSizeNode>(*other.firstChild) : nullptr;
+    secondChild = other.secondChild ? std::make_unique<LayoutSizeNode>(*other.secondChild) : nullptr;
+    nextFirstChild = other.nextFirstChild ? std::make_unique<LayoutSizeNode>(*other.nextFirstChild) : nullptr;
+    nextSecondChild = other.nextSecondChild ? std::make_unique<LayoutSizeNode>(*other.nextSecondChild) : nullptr;
 
     return *this;
 }
-
-// Method Description:
-// - Performs assignment operation on a single child node reusing
-// - current one if present.
-// Arguments:
-// - nodeField: Reference to our field holding concerned node.
-// - other: Node to take the values from.
-// Return Value:
-// - <none>
-void Pane::LayoutSizeNode::_AssignChildNode(std::unique_ptr<LayoutSizeNode>& nodeField, const LayoutSizeNode* const newNode)
-{
-    if (newNode)
-    {
-        if (nodeField)
-        {
-            *nodeField = *newNode;
-        }
-        else
-        {
-            nodeField = std::make_unique<LayoutSizeNode>(*newNode);
-        }
-    }
-    else
-    {
-        nodeField.release();
-    }
-}
diff --git a/src/cascadia/TerminalApp/Pane.h b/src/cascadia/TerminalApp/Pane.h
index a163ba2b4b5..cc844743860 100644
--- a/src/cascadia/TerminalApp/Pane.h
+++ b/src/cascadia/TerminalApp/Pane.h
@@ -394,9 +394,6 @@ class Pane : public std::enable_shared_from_this<Pane>
         LayoutSizeNode(const LayoutSizeNode& other);
 
         LayoutSizeNode& operator=(const LayoutSizeNode& other);
-
-    private:
-        void _AssignChildNode(std::unique_ptr<LayoutSizeNode>& nodeField, const LayoutSizeNode* const newNode);
     };
 
     friend struct winrt::TerminalApp::implementation::TerminalTab;


Test patch: