Skip to content

Commit c1cf1c0

Browse files
author
mikeblome
committed
fixed GH 317
1 parent b0e39bc commit c1cf1c0

2 files changed

Lines changed: 63 additions & 8 deletions

File tree

docs/cpp/codesnippet/CPP/how-to-create-and-use-weak-ptr-instances_1.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ void RunTest()
5656
// Give each controller a pointer to all the others.
5757
for (int i = 0 ; i < v.size(); ++i)
5858
{
59-
for_each(v.begin(), v.end(), [v,i] (shared_ptr<Controller> p)
59+
for_each(v.begin(), v.end(), [&v,i] (shared_ptr<Controller> p)
6060
{
6161
if(p->Num != i)
6262
{

docs/cpp/how-to-create-and-use-weak-ptr-instances.md

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,74 @@ ms.workload: ["cplusplus"]
1212
---
1313
# How to: Create and Use weak_ptr Instances
1414
Sometimes an object must store a way to access the underlying object of a `shared_ptr` without causing the reference count to be incremented. Typically, this situation occurs when you have cyclic references between `shared_ptr` instances.
15-
15+
1616
The best design is to avoid shared ownership of pointers whenever you can. However, if you must have shared ownership of `shared_ptr` instances, avoid cyclic references between them. When cyclic references are unavoidable, or even preferable for some reason, use `weak_ptr` to give one or more of the owners a weak reference to another `shared_ptr`. By using a `weak_ptr`, you can create a `shared_ptr` that joins to an existing set of related instances, but only if the underlying memory resource is still valid. A `weak_ptr` itself does not participate in the reference counting, and therefore, it cannot prevent the reference count from going to zero. However, you can use a `weak_ptr` to try to obtain a new copy of the `shared_ptr` with which it was initialized. If the memory has already been deleted, a `bad_weak_ptr` exception is thrown. If the memory is still valid, the new shared pointer increments the reference count and guarantees that the memory will be valid as long as the `shared_ptr` variable stays in scope.
17-
17+
1818
## Example
1919
The following code example shows a case where `weak_ptr` is used to ensure proper deletion of objects that have circular dependencies. As you examine the example, assume that it was created only after alternative solutions were considered. The `Controller` objects represent some aspect of a machine process, and they operate independently. Each controller must be able to query the status of the other controllers at any time, and each one contains a private `vector<weak_ptr<Controller>>` for this purpose. Each vector contains a circular reference, and therefore, `weak_ptr` instances are used instead of `shared_ptr`.
20-
20+
2121
[!code-cpp[stl_smart_pointers#222](../cpp/codesnippet/CPP/how-to-create-and-use-weak-ptr-instances_1.cpp)]
22-
22+
2323
```Output
24-
Creating Controller0Creating Controller1Creating Controller2Creating Controller3Creating Controller4push_back to v[0]: 1push_back to v[0]: 2push_back to v[0]: 3push_back to v[0]: 4push_back to v[1]: 0push_back to v[1]: 2push_back to v[1]: 3push_back to v[1]: 4push_back to v[2]: 0push_back to v[2]: 1push_back to v[2]: 3push_back to v[2]: 4push_back to v[3]: 0push_back to v[3]: 1push_back to v[3]: 2push_back to v[3]: 4push_back to v[4]: 0push_back to v[4]: 1push_back to v[4]: 2push_back to v[4]: 3use_count = 1Status of 1 = OnStatus of 2 = OnStatus of 3 = OnStatus of 4 = Onuse_count = 1Status of 0 = OnStatus of 2 = OnStatus of 3 = OnStatus of 4 = Onuse_count = 1Status of 0 = OnStatus of 1 = OnStatus of 3 = OnStatus of 4 = Onuse_count = 1Status of 0 = OnStatus of 1 = OnStatus of 2 = OnStatus of 4 = Onuse_count = 1Status of 0 = OnStatus of 1 = OnStatus of 2 = OnStatus of 3 = OnDestroying Controller0Destroying Controller1Destroying Controller2Destroying Controller3Destroying Controller4Press any key
24+
Creating Controller0
25+
Creating Controller1
26+
Creating Controller2
27+
Creating Controller3
28+
Creating Controller4
29+
push_back to v[0]: 1
30+
push_back to v[0]: 2
31+
push_back to v[0]: 3
32+
push_back to v[0]: 4
33+
push_back to v[1]: 0
34+
push_back to v[1]: 2
35+
push_back to v[1]: 3
36+
push_back to v[1]: 4
37+
push_back to v[2]: 0
38+
push_back to v[2]: 1
39+
push_back to v[2]: 3
40+
push_back to v[2]: 4
41+
push_back to v[3]: 0
42+
push_back to v[3]: 1
43+
push_back to v[3]: 2
44+
push_back to v[3]: 4
45+
push_back to v[4]: 0
46+
push_back to v[4]: 1
47+
push_back to v[4]: 2
48+
push_back to v[4]: 3
49+
use_count = 1
50+
Status of 1 = On
51+
Status of 2 = On
52+
Status of 3 = On
53+
Status of 4 = On
54+
use_count = 1
55+
Status of 0 = On
56+
Status of 2 = On
57+
Status of 3 = On
58+
Status of 4 = On
59+
use_count = 1
60+
Status of 0 = On
61+
Status of 1 = On
62+
Status of 3 = On
63+
Status of 4 = On
64+
use_count = 1
65+
Status of 0 = O
66+
nStatus of 1 = On
67+
Status of 2 = On
68+
Status of 4 = On
69+
use_count = 1
70+
Status of 0 = On
71+
Status of 1 = On
72+
Status of 2 = On
73+
Status of 3 = On
74+
Destroying Controller0
75+
Destroying Controller1
76+
Destroying Controller2
77+
Destroying Controller3
78+
Destroying Controller4
79+
Press any key
2580
```
26-
81+
2782
As an experiment, modify the vector `others` to be a `vector<shared_ptr<Controller>>`, and then in the output, notice that no destructors are invoked when `TestRun` returns.
28-
83+
2984
## See Also
3085
[Smart Pointers](../cpp/smart-pointers-modern-cpp.md)

0 commit comments

Comments
 (0)