diff options
| author | 2021-03-05 22:10:03 -0800 | |
|---|---|---|
| committer | 2021-03-05 22:10:03 -0800 | |
| commit | 68ffac250ae2d1c2d5d961304d2c61f6755ab033 (patch) | |
| tree | 4888d0fd2474dc90ad84a17ba71010a325beb984 /src/common/fiber.cpp | |
| parent | Merge pull request #6036 from bunnei/thread-leak (diff) | |
| download | yuzu-68ffac250ae2d1c2d5d961304d2c61f6755ab033.tar.gz yuzu-68ffac250ae2d1c2d5d961304d2c61f6755ab033.tar.xz yuzu-68ffac250ae2d1c2d5d961304d2c61f6755ab033.zip | |
common: fiber: Use weak_ptr when yielding.
- Avoids a memory leak, as taking a strong reference of the fiber here causes a circular reference.
- Supersedes #6006 with a more narrow fix.
Diffstat (limited to 'src/common/fiber.cpp')
| -rw-r--r-- | src/common/fiber.cpp | 19 |
1 files changed, 12 insertions, 7 deletions
diff --git a/src/common/fiber.cpp b/src/common/fiber.cpp index 3c1eefcb7..b06fdc258 100644 --- a/src/common/fiber.cpp +++ b/src/common/fiber.cpp | |||
| @@ -116,16 +116,21 @@ void Fiber::Rewind() { | |||
| 116 | boost::context::detail::jump_fcontext(impl->rewind_context, this); | 116 | boost::context::detail::jump_fcontext(impl->rewind_context, this); |
| 117 | } | 117 | } |
| 118 | 118 | ||
| 119 | void Fiber::YieldTo(std::shared_ptr<Fiber> from, std::shared_ptr<Fiber> to) { | 119 | void Fiber::YieldTo(std::weak_ptr<Fiber> weak_from, std::shared_ptr<Fiber> to) { |
| 120 | ASSERT_MSG(from != nullptr, "Yielding fiber is null!"); | ||
| 121 | ASSERT_MSG(to != nullptr, "Next fiber is null!"); | 120 | ASSERT_MSG(to != nullptr, "Next fiber is null!"); |
| 121 | |||
| 122 | to->impl->guard.lock(); | 122 | to->impl->guard.lock(); |
| 123 | to->impl->previous_fiber = from; | 123 | to->impl->previous_fiber = weak_from.lock(); |
| 124 | |||
| 124 | auto transfer = boost::context::detail::jump_fcontext(to->impl->context, to.get()); | 125 | auto transfer = boost::context::detail::jump_fcontext(to->impl->context, to.get()); |
| 125 | ASSERT(from->impl->previous_fiber != nullptr); | 126 | |
| 126 | from->impl->previous_fiber->impl->context = transfer.fctx; | 127 | // "from" might no longer be valid if the thread was killed |
| 127 | from->impl->previous_fiber->impl->guard.unlock(); | 128 | if (auto from = weak_from.lock(); from != nullptr) { |
| 128 | from->impl->previous_fiber.reset(); | 129 | ASSERT(from->impl->previous_fiber != nullptr); |
| 130 | from->impl->previous_fiber->impl->context = transfer.fctx; | ||
| 131 | from->impl->previous_fiber->impl->guard.unlock(); | ||
| 132 | from->impl->previous_fiber.reset(); | ||
| 133 | } | ||
| 129 | } | 134 | } |
| 130 | 135 | ||
| 131 | std::shared_ptr<Fiber> Fiber::ThreadToFiber() { | 136 | std::shared_ptr<Fiber> Fiber::ThreadToFiber() { |