| 180 | } |
| 181 | |
| 182 | void memory_test::test_weak_ptr() |
| 183 | { |
| 184 | mock::clear(); |
| 185 | { |
| 186 | std::weak_ptr<mock> wptr1{}; |
| 187 | { |
| 188 | auto ptr1 = std::make_shared<mock>(); |
| 189 | wptr1 = ptr1; |
| 190 | { |
| 191 | std::weak_ptr<mock> wptr2 = ptr1; |
| 192 | test_eq("weak_ptr shares use count", wptr2.use_count(), 1L); |
| 193 | test_eq("weak_ptr not expired", wptr2.expired(), false); |
| 194 | |
| 195 | auto ptr2 = wptr2.lock(); |
| 196 | test_eq("locked ptr increases use count", ptr1.use_count(), 2L); |
| 197 | } |
| 198 | } |
| 199 | test_eq("weak_ptr expired after all shared_ptrs die", wptr1.expired(), true); |
| 200 | test_eq("shared object destroyed while weak_ptr exists", mock::destructor_calls, 1U); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | void memory_test::test_allocators() |
| 205 | { |