-
-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is passing objects as reference from a coroutine to a coroutine function ok? #131
Comments
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 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
IF the 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 |
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 |
I should document this better in the docs :-) |
An example:
QCoro::Task<> MyClass::doSomethingWithData(QByteArray &data) {
}
QCoro::Task<> MyClass::fetchData() {
}
The text was updated successfully, but these errors were encountered: