Fix memory leak in CBridgeGen (#3243)

Affecting String and CValuesRef conversions when calling variadic C functions
This commit is contained in:
SvyatoslavScherbina
2019-08-12 14:02:39 +03:00
committed by GitHub
parent 65e6f85dc1
commit e869e40596
@@ -159,7 +159,7 @@ internal class KotlinCBridgeBuilder(
internal class KotlinCallBuilder(private val irBuilder: IrBuilderWithScope, private val symbols: KonanSymbols) { internal class KotlinCallBuilder(private val irBuilder: IrBuilderWithScope, private val symbols: KonanSymbols) {
val prepare = mutableListOf<IrStatement>() val prepare = mutableListOf<IrStatement>()
val arguments = mutableListOf<IrExpression>() val arguments = mutableListOf<IrExpression>()
val cleanup = mutableListOf<IrStatement>() val cleanup = mutableListOf<IrBuilderWithScope.() -> IrStatement>()
private var memScope: IrVariable? = null private var memScope: IrVariable? = null
@@ -172,8 +172,10 @@ internal class KotlinCallBuilder(private val irBuilder: IrBuilderWithScope, priv
prepare += newMemScope prepare += newMemScope
val clearImpl = symbols.interopMemScope.owner.simpleFunctions().single { it.name.asString() == "clearImpl" } val clearImpl = symbols.interopMemScope.owner.simpleFunctions().single { it.name.asString() == "clearImpl" }
cleanup += irCall(clearImpl).apply { cleanup += {
dispatchReceiver = irGet(memScope!!) irCall(clearImpl).apply {
dispatchReceiver = irGet(memScope!!)
}
} }
irGet(newMemScope) irGet(newMemScope)
@@ -207,15 +209,18 @@ internal class KotlinCallBuilder(private val irBuilder: IrBuilderWithScope, priv
+kotlinCall +kotlinCall
} else { } else {
// Note: generating try-catch as finally blocks are already lowered. // Note: generating try-catch as finally blocks are already lowered.
+IrTryImpl(startOffset, endOffset, kotlinCall.type).apply { val result = irTemporary(IrTryImpl(startOffset, endOffset, kotlinCall.type).apply {
tryResult = kotlinCall tryResult = kotlinCall
catches += irCatch(context.irBuiltIns.throwableType).apply { catches += irCatch(context.irBuiltIns.throwableType).apply {
result = irBlock(kotlinCall) { result = irBlock(kotlinCall) {
cleanup.forEach { +it } cleanup.forEach { +it() }
+irThrow(irGet(catchParameter)) +irThrow(irGet(catchParameter))
} }
} }
} })
// TODO: consider handling a cleanup failure properly.
cleanup.forEach { +it() }
+irGet(result)
} }
} }
} }