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

Breaking Circular References With Weak PointersBreaking Circular References With Weak Pointers

As we saw in the previous section, circular references occur when two (or more) shared pointers reference each other in a loop, creating a deadly cycle. This interdependence prevents the destructor of the object from being called, as the reference counts never reach zero. The result is a very common memory leak which became the primary reason why weak pointers were introduced.

Breaking the cycle with weak pointers

Weak pointers are purpose-built to break these cycles. By replacing a shared pointer in a circular reference with a weak pointer, we can ensure that one of the objects does not artificially extend the lifetime of another in a manner that would prevent either from being destroyed.

To understand this concept better, let’s rewrite the linked-list circular reference problem we discussed in the previous section using a weak pointer.

cpp

main.cpp

In the above code, we fixed our circular reference problem by changing next from being a shared pointer to a weak pointer. Follow the comments in the above code for better understanding. And don’t forget to run the code to see the destructors being called now. No memory leaks!

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

Selecione a resposta correta

Tudo estava claro?

Seção 4. Capítulo 3
course content

Conteúdo do Curso

C++ Smart Pointers

Breaking Circular References With Weak PointersBreaking Circular References With Weak Pointers

As we saw in the previous section, circular references occur when two (or more) shared pointers reference each other in a loop, creating a deadly cycle. This interdependence prevents the destructor of the object from being called, as the reference counts never reach zero. The result is a very common memory leak which became the primary reason why weak pointers were introduced.

Breaking the cycle with weak pointers

Weak pointers are purpose-built to break these cycles. By replacing a shared pointer in a circular reference with a weak pointer, we can ensure that one of the objects does not artificially extend the lifetime of another in a manner that would prevent either from being destroyed.

To understand this concept better, let’s rewrite the linked-list circular reference problem we discussed in the previous section using a weak pointer.

cpp

main.cpp

In the above code, we fixed our circular reference problem by changing next from being a shared pointer to a weak pointer. Follow the comments in the above code for better understanding. And don’t forget to run the code to see the destructors being called now. No memory leaks!

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

Selecione a resposta correta

Tudo estava claro?

Seção 4. Capítulo 3
some-alt