Skip to content

Commit

Permalink
Memory leak in initPsiFileFactory (pinterest#1407)
Browse files Browse the repository at this point in the history
* Explicitly dispose to (possibly) prevent a memory leak

Possibly closes pinterest#1216
  • Loading branch information
paul-dingemans authored Mar 13, 2022
1 parent 658e574 commit 4646371
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ This section is applicable when providing rules that depend on one or more value
- Do not delete blank lines in KDoc (no-trailing-spaces) ([#1376](https://github.com/pinterest/ktlint/issues/1376))
- Do not indent raw string literals that are not followed by either trimIndent() or trimMargin() (`indent`) ([#1375](https://github.com/pinterest/ktlint/issues/1375))
- Revert remove unnecessary wildcard imports as introduced in Ktlint 0.43.0 (`no-unused-imports`) ([#1277](https://github.com/pinterest/ktlint/issues/1277)), ([#1393](https://github.com/pinterest/ktlint/issues/1393)), ([#1256](https://github.com/pinterest/ktlint/issues/1256))
- (Possibly) resolve memory leak ([#1216](https://github.com/pinterest/ktlint/issues/1216))

### Changed
- Print the rule id always in the PlainReporter ([#1121](https://github.com/pinterest/ktlint/issues/1121))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,22 @@ internal fun initPsiFileFactory(isFromCli: Boolean): PsiFileFactory {
}

val disposable = Disposer.newDisposable()

val project = KotlinCoreEnvironment.createForProduction(
disposable,
compilerConfiguration,
EnvironmentConfigFiles.JVM_CONFIG_FILES
).project as MockProject

project.enableASTMutations()

return PsiFileFactory.getInstance(project)
try {
val project = KotlinCoreEnvironment.createForProduction(
disposable,
compilerConfiguration,
EnvironmentConfigFiles.JVM_CONFIG_FILES
).project as MockProject

project.enableASTMutations()

return PsiFileFactory.getInstance(project)
} finally {
// Dispose explicitly to (possibly) prevent memory leak
// https://discuss.kotlinlang.org/t/memory-leak-in-kotlincoreenvironment-and-kotlintojvmbytecodecompiler/21950
// https://youtrack.jetbrains.com/issue/KT-47044
disposable.dispose()
}
}

/**
Expand Down

0 comments on commit 4646371

Please sign in to comment.