CodexBloom - Programming Q&A Platform

Segmentation fault when using std::unique_ptr in a custom tree structure in C++17

👀 Views: 51 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-11
c++ memory-management unique-pointer binary-tree C++

I've been banging my head against this for hours... I'm working on a project and hit a roadblock... I've been banging my head against this for hours... I'm working with a segmentation fault when I attempt to delete nodes in a custom binary tree structure using `std::unique_ptr`. The tree is designed to be a simple binary search tree. When I try to delete a node, the program crashes, but it seems to only happen under certain conditions. Here's a simplified version of my code: ```cpp #include <iostream> #include <memory> struct Node { int value; std::unique_ptr<Node> left; std::unique_ptr<Node> right; Node(int v) : value(v), left(nullptr), right(nullptr) {} }; class BinaryTree { public: std::unique_ptr<Node> root; void insert(int value) { if (!root) { root = std::make_unique<Node>(value); } else { insertRec(root.get(), value); } } void insertRec(Node* node, int value) { if (value < node->value) { if (node->left) insertRec(node->left.get(), value); else node->left = std::make_unique<Node>(value); } else { if (node->right) insertRec(node->right.get(), value); else node->right = std::make_unique<Node>(value); } } void deleteNode(int value) { root = deleteRec(std::move(root), value); } private: std::unique_ptr<Node> deleteRec(std::unique_ptr<Node> node, int value) { if (!node) return nullptr; if (value < node->value) { node->left = deleteRec(std::move(node->left), value); } else if (value > node->value) { node->right = deleteRec(std::move(node->right), value); } else { // Node with only one child or no child if (!node->left) { return std::move(node->right); } else if (!node->right) { return std::move(node->left); } // Node with two children: Get the inorder successor (smallest in the right subtree) Node* successor = minValueNode(node->right.get()); node->value = successor->value; node->right = deleteRec(std::move(node->right), successor->value); } return std::move(node); } Node* minValueNode(Node* node) { Node* current = node; while (current && current->left) { current = current->left.get(); } return current; } }; int main() { BinaryTree tree; tree.insert(10); tree.insert(5); tree.insert(15); tree.deleteNode(10); tree.deleteNode(5); return 0; } ``` The segmentation fault occurs when I attempt to delete nodes, but I need to determine why. I've checked that the unique pointers should be properly managed, and I'm careful with ownership. The function `deleteRec` is supposed to handle the deletion but it seems like I'm either dereferencing a null pointer or the tree structure gets corrupted somehow. I've tried adding debug statements to check the states of the nodes before and after deletion, but I still need to pinpoint the scenario. Any insights on what might be causing this fault or how to debug it effectively? This is part of a larger web app I'm building. For context: I'm using C++ on Linux. What am I doing wrong?