Code Monkey home page Code Monkey logo

Comments (3)

danvratil avatar danvratil commented on June 29, 2024

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_awaited inside the fetchData() coroutine, so the following things happen:

  1. the data variable is allocated and initialized with result of reply->readAll()
  2. the doSomethingWithData() coroutine is called, passing it reference to data
  3. at this point, accessing the data reference inside doSomethingWithData() coroutine is completely safe
  4. the doSomethingWithData() coroutine is suspended when co_awaiting the network reply
  5. execution returns back to fetchData()
  6. fetchData() coroutine reaches the end of the function body, data is destroyed, networkAccessManager is destroyed, reply leaks ;-)
  7. at some point later, doSomethingWithData() coroutine is resumed
  8. at this point, data inside doSomethingWithData() 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_awaited 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.

BurievSardor avatar BurievSardor commented on June 29, 2024

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.

danvratil avatar danvratil commented on June 29, 2024

I should document this better in the docs :-)

from qcoro.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.