Comments (3)
Generally speaking, yes. It is OK to use references as coroutine arguments, but one has to be careful about the lifetime of the referenced object.
The example above may or may not be safe, since you did not show how you are using the data
reference in doSomethingWithData()
.
Let me expand on your example:
QCoro::Task<> MyClass::doSomethingWithData(QByteArray &data) {
QNetworkAccessManager networkAccessManager;
// It's perfectly safe to use `data` up to this point
// Actually, it's safe to use is on the next line as well, but only in the
// expression *after* the `co_await` keyword:
// v---------------------------v
const QNetworkReply *reply = co_await networkAccessManager.get(url);
// It's *NOT* safe to use `data` from this point on. The reference is dangling.
}
The reason is that the doSomethingWithData()
coroutine is not co_await
ed inside the fetchData()
coroutine, so the following things happen:
- the
data
variable is allocated and initialized with result ofreply->readAll()
- the
doSomethingWithData()
coroutine is called, passing it reference todata
- at this point, accessing the
data
reference insidedoSomethingWithData()
coroutine is completely safe - the
doSomethingWithData()
coroutine is suspended whenco_await
ing the network reply - execution returns back to
fetchData()
fetchData()
coroutine reaches the end of the function body,data
is destroyed,networkAccessManager
is destroyed,reply
leaks ;-)- at some point later,
doSomethingWithData()
coroutine is resumed - at this point,
data
insidedoSomethingWithData()
coroutine is a dangling reference and must NOT be used, because the object that it references has been already destroyed in step 6.
IF the doSomethingWithData()
coroutine were co_await
ed inside fetchData()
, then it would be totally safe to use the data
reference anywhere inside the doSomethingWithData()
coroutine, since the fetchData()
coroutine would not be finished until the doSomethingWithData()
coroutine would finish, and thus it would be guaranteed that the data
object outlives the data
reference.
To sum it up, yes, it's safe to pass arguments to coroutines as references, but one must be careful when using references whose lifespan crosses a suspension point (a co_await
), as the may become dangling references while the coroutine is suspended.
from qcoro.
Thank you for your immediate response and explanation. I now understand how to safely pass objects by reference to a coroutine function inside another coroutine function. It's my mistake that I forgot to add co_await
keyword before doSomethingWithData() function.
from qcoro.
I should document this better in the docs :-)
from qcoro.
Related Issues (20)
- `CheckAtomic.cmake` Fails on Windows with LLVM Clang
- Build Failure with Clang `15.x`< on Windows in Debug
- QCoroSignal crashes
- QCoro::waitFor does not rethrow exceptions
- Add latest Qt6 to CI
- Bind coroutine to a QObject lifetime HOT 6
- Make not co_awaiting a coroutine trigger -Wunused-result HOT 1
- Add this library to vcpkg HOT 1
- Apple Clang ICEs in Release build of QCoro HOT 1
- Update QCoro in ConanCenter
- Add support to work with database HOT 2
- Next release HOT 3
- ASSERT: "this->mAwaitedCoroutine != nullptr" when awaiting a default-constructed Task HOT 4
- like winrt HOT 2
- support mqtt HOT 2
- Qt Design Studio : Mocking QCoro::QmlTask in Javascript or QML HOT 3
- Clang 14.x ornery about std::experimental::coroutine_traits HOT 3
- Illogical result of the `write` function.
- Bug: application crashes when signal from another thread timeouts HOT 1
- Support for move only types in QFuture via takeResult
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from qcoro.