Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Breaking Circular References With Weak Pointers | Weak Pointers
C++ Smart Pointers

bookBreaking Circular References With Weak Pointers

Weak pointers are designed to break circular references. By replacing a std::shared_ptr in a circular dependency with a std::weak_ptr, we prevent objects from unintentionally extending each other's lifetimes, allowing proper destruction.

main.cpp

main.cpp

copy
12345678910111213141516171819202122232425
#include <iostream> #include <memory> class Node { public: Node() { std::cout << "Node constructed." << std::endl; } ~Node() { std::cout << "Node destructed." << std::endl; } // A weak pointer to the next element prevents circular ownership. std::weak_ptr<Node> next; }; int main() { // Creating three Node objects. std::shared_ptr<Node> node1 = std::make_shared<Node>(); std::shared_ptr<Node> node2 = std::make_shared<Node>(); std::shared_ptr<Node> node3 = std::make_shared<Node>(); // Creating a list where the last node's next is a weak pointer node1->next = node2; node2->next = node3; node3->next = node1; // Now when node1, node2, and node3 go out of scope, their destructors will be called }

The circular reference problem is resolved by changing next from a std::shared_ptr to a std::weak_ptr. Follow the comments in the code for a clearer understanding. Run the code to confirm that the destructors are now called properly no memory leaks!

question mark

The following code has two shared pointers A and B both sharing a resource. Modify it so that B becomes a weak pointer.

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 4. Kapitel 3

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Suggested prompts:

Can you explain how circular references cause memory leaks?

Can you show an example of code with and without weak pointers?

What is the difference between std::shared_ptr and std::weak_ptr?

Awesome!

Completion rate improved to 5.56

bookBreaking Circular References With Weak Pointers

Svep för att visa menyn

Weak pointers are designed to break circular references. By replacing a std::shared_ptr in a circular dependency with a std::weak_ptr, we prevent objects from unintentionally extending each other's lifetimes, allowing proper destruction.

main.cpp

main.cpp

copy
12345678910111213141516171819202122232425
#include <iostream> #include <memory> class Node { public: Node() { std::cout << "Node constructed." << std::endl; } ~Node() { std::cout << "Node destructed." << std::endl; } // A weak pointer to the next element prevents circular ownership. std::weak_ptr<Node> next; }; int main() { // Creating three Node objects. std::shared_ptr<Node> node1 = std::make_shared<Node>(); std::shared_ptr<Node> node2 = std::make_shared<Node>(); std::shared_ptr<Node> node3 = std::make_shared<Node>(); // Creating a list where the last node's next is a weak pointer node1->next = node2; node2->next = node3; node3->next = node1; // Now when node1, node2, and node3 go out of scope, their destructors will be called }

The circular reference problem is resolved by changing next from a std::shared_ptr to a std::weak_ptr. Follow the comments in the code for a clearer understanding. Run the code to confirm that the destructors are now called properly no memory leaks!

question mark

The following code has two shared pointers A and B both sharing a resource. Modify it so that B becomes a weak pointer.

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 4. Kapitel 3
some-alt