• flashgnash@lemm.ee
    link
    fedilink
    arrow-up
    4
    ·
    20 days ago

    Thought the whole point was that it forced you to handle memory properly and automatically released things when they go out of scope

    What kind of situation can cause a memory leak in rust

    • naonintendois@programming.dev
      link
      fedilink
      arrow-up
      4
      ·
      20 days ago

      You can have a memory leak when items are still in scope in some loop or when you have a reference count cycle. The latter happens with the Rc/Arc types in rust.

      An example for the former can be a web server that keeps track of every request it’s ever received in memory. You will eventually run out of memory. But you did not violate any memory rules (dangling pointer, etc.). Memory leaks can be caused by design issues.

      • flashgnash@lemm.ee
        link
        fedilink
        arrow-up
        1
        ·
        20 days ago

        That doesn’t fit the definition of memory leak in my mind, had thought a memory leak was specifically when the program completely loses track of memory

        • MoonMelon@lemmy.ml
          link
          fedilink
          English
          arrow-up
          4
          arrow-down
          1
          ·
          edit-2
          20 days ago

          That’s one kind, and Rust’s “ownership” concept does mean there’s built-in compile time checks to prevent dangling pointers or unreachable memory. But there’s also just never de-allocating stuff you allocated even though it’s still reachable. Like you could just make a loop that allocates memory and never stops and that’s a memory leak, or more generally a “resource leak”, if you prefer.

          Rust is really good at keeping you from having a reference to something that you think is valid but it turns out it got mutated way down in some class hierarchy and now it’s dead, so you have a null pointer or you double free, or whatever. But it can’t stop the case where your code is technically valid but the resource leak is caused by bad “logic” in your design, if that makes sense.