you are viewing a single comment's thread
view the rest of the comments
[–] 21 points 1 month ago (8 children)

Garbage collection doesn't solve the problem either, cause it's too unpredictable and typically doesn't even give you the option to close resources when memory is reclaimed. So just opening SQL connections and forgetting to close them will quickly drain the connection pool.

  • source
  • parent
  • hideshow 8 child comments
  • [–] 9 points 1 month ago (7 children)

    Seems like someone doesn't RAII their connections!

  • source
  • parent
  • hideshow 7 child comments
  • [–] 12 points 1 month ago* (6 children)

    You can't, not in e.g. Java or C#.

    C++ and Rust are the only languages I know of that do this right.

  • source
  • parent
  • hideshow 6 child comments
  • [–] 7 points 1 month ago (2 children)

    It's not impossible, at least in C# I know you can mark objects that need to be RAII with some custom attribute and enforce correct usage patterns with a custom analyzer. You need to write it yourself but it's not impossible that way.

  • source
  • parent
  • hideshow 2 child comments
  • [–] 4 points 1 month ago (1 child)

    Maybe you're thinking of IDisposable with the using keyword. The problem with that is that you have to remember to use it in every call site. Nothing stops a caller from writing var x = new Foo() and walking away.

  • source
  • parent
  • hideshow 1 child comment
  • [–] 1 point 1 month ago (1 child)

    You can do some kind of RAII in python with the with keyword. Only works for local variables though, where it's easy to free resources manually

  • source
  • parent
  • hideshow 1 child comment
  • [–] 1 point 1 month ago* (last edited 1 month ago)

    But if you ever forget the with keyword, you have a leak. With proper RAII the resource acquisition and release is a property of the object's type and impossible to forget. RAII doesn't just mean "resource acquisition is initialization", it also implies "destruction is resource release".

  • source
  • parent