title: Kotlin : safe lambdas (no memory leak)?
body: <p>After having read <a href="https://medium.com/freenet-engineering/memory-leaks-in-android-identify-treat-and-avoid-d0b1233acc8#.yet4778q1" rel="noreferrer">this article about Memory Leaks</a>, I am wondering whether using lambdas in Kotlin Android project is safe. It's true that lambda syntax makes me program with more ease, but what about the Memory Leaks ?</p>
  
  <p>As an example of the problematic, I've taken a piece of code from one of my projects, where I build an AlertDialog. This code is inside the MainActivity class of my project.</p>
  
  <pre><code>fun deleteItemOnConfirmation(id: Long) : Unit {
          val item = explorerAdapter.getItemAt(id.toInt())
          val stringId = if (item.isDirectory) R.string.about_to_delete_folder else R.string.about_to_delete_file
  
          val dialog = AlertDialog.Builder(this).
                  setMessage(String.format(getString(stringId), item.name)).setPositiveButton(
                  R.string.ok, {dialog: DialogInterface, id: Int -&gt;
                          val success = if (item.isDirectory) ExplorerFileManager.deleteFolderRecursively(item.name)
                          else ExplorerFileManager.deleteFile(item.name)
                          if (success) {
                              explorerAdapter.deleteItem(item)
                              explorerRecyclerView.invalidate()
                          }
                          else Toast.makeText(this@MainActivity, R.string.file_deletion_error, Toast.LENGTH_SHORT).show()
                      }).setNegativeButton(
                  R.string.cancel, {dialog: DialogInterface, id: Int -&gt;
                      dialog.cancel()
          })
  
          dialog.show()
  }
  </code></pre>
  
  <p>My question is very simple : can the two lambdas set for positive and negative buttons lead to Memory Leaks ? (I also mean, are kotlin lambdas simply converted to Java Anonymous functions ?)</p>
  
  <p>Edit : Maybe I've got my answer <a href="https://discuss.kotlinlang.org/t/function-literals-and-reference-to-enclosing-class/416" rel="noreferrer">in this Jetbrains Topic</a>.</p>