Move shared pointers in dtors

release/4.3a0
Matt Morley 2025-01-19 20:55:51 -08:00
parent 6697452954
commit b3c02d2b40
3 changed files with 16 additions and 18 deletions

View File

@ -196,21 +196,20 @@ namespace gtsam {
for (auto&& root: roots_) {
std::queue<sharedClique> bfs_queue;
// first, move the root to the queue
bfs_queue.push(root);
root = nullptr; // now the root node is owned by the queue
// first, steal the root and move it to the queue. This invalidates root
bfs_queue.push(std::move(root));
// do a BFS on the tree, for each node, add its children to the queue, and then delete it from the queue
// So if the reference count of the node is 1, it will be deleted, and because its children are in the queue,
// the deletion of the node will not trigger a recursive deletion of the children.
while (!bfs_queue.empty()) {
// move the ownership of the front node from the queue to the current variable
auto current = bfs_queue.front();
// move the ownership of the front node from the queue to the current variable, invalidating the sharedClique at the front of the queue
auto current = std::move(bfs_queue.front());
bfs_queue.pop();
// add the children of the current node to the queue, so that the queue will also own the children nodes.
for (auto child: current->children) {
bfs_queue.push(child);
bfs_queue.push(std::move(child));
} // leaving the scope of current will decrease the reference count of the current node by 1, and if the reference count is 0,
// the node will be deleted. Because the children are in the queue, the deletion of the node will not trigger a recursive
// deletion of the children.

View File

@ -114,20 +114,20 @@ ClusterTree<GRAPH>::~ClusterTree() {
for (auto&& root : roots_) {
std::queue<sharedNode> bfs_queue;
// first, move the root to the queue
bfs_queue.push(root);
root = nullptr; // now the root node is owned by the queue
// first, steal the root and move it to the queue. This invalidates root
bfs_queue.push(std::move(root));
// for each node iterated, if its reference count is 1, it will be deleted while its children are still in the queue.
// so that the recursive deletion will not happen.
while (!bfs_queue.empty()) {
// move the ownership of the front node from the queue to the current variable
auto node = bfs_queue.front();
// move the ownership of the front node from the queue to the current variable, invalidating the sharedClique at the front of the queue
auto node = std::move(bfs_queue.front());
bfs_queue.pop();
// add the children of the current node to the queue, so that the queue will also own the children nodes.
for (auto child : node->children) {
bfs_queue.push(child);
bfs_queue.push(std::move(child));
} // leaving the scope of current will decrease the reference count of the current node by 1, and if the reference count is 0,
// the node will be deleted. Because the children are in the queue, the deletion of the node will not trigger a recursive
// deletion of the children.

View File

@ -197,20 +197,19 @@ namespace gtsam {
for (auto&& root : roots_) {
std::queue<sharedNode> bfs_queue;
// first, move the root to the queue
bfs_queue.push(root);
root = nullptr; // now the root node is owned by the queue
// first, steal the root and move it to the queue. This invalidates root
bfs_queue.push(std::move(root));
// for each node iterated, if its reference count is 1, it will be deleted while its children are still in the queue.
// so that the recursive deletion will not happen.
while (!bfs_queue.empty()) {
// move the ownership of the front node from the queue to the current variable
auto node = bfs_queue.front();
// move the ownership of the front node from the queue to the current variable, invalidating the sharedClique at the front of the queue
auto node = std::move(bfs_queue.front());
bfs_queue.pop();
// add the children of the current node to the queue, so that the queue will also own the children nodes.
for (auto&& child : node->children) {
bfs_queue.push(child);
bfs_queue.push(std::move(child));
} // leaving the scope of current will decrease the reference count of the current node by 1, and if the reference count is 0,
// the node will be deleted. Because the children are in the queue, the deletion of the node will not trigger a recursive
// deletion of the children.