Skip to content

Commit 93eb261

Browse files
authored
Merge pull request #12546 from slavapestov/alloc-ref-dynamic-leak
SILGen: Fix memory leak when calling constructor requirement of @objc protocol
2 parents e8be4fb + 045c649 commit 93eb261

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

lib/SILGen/SILGenApply.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -917,10 +917,9 @@ class SILGenApply : public Lowering::ExprVisitor<SILGenApply> {
917917
std::tie(selfMetaObjC, instanceType) = convertToObjCMetatype(selfMeta, loc);
918918

919919
// Allocate the object.
920-
return ManagedValue(
920+
return SGF.emitManagedRValueWithCleanup(
921921
SGF.B.createAllocRefDynamic(loc, selfMetaObjC.getValue(), instanceType,
922-
/*objc=*/true, {}, {}),
923-
selfMetaObjC.getCleanup());
922+
/*objc=*/true, {}, {}));
924923
}
925924

926925
void processProtocolDecl(DeclRefExpr *e, AbstractFunctionDecl *afd,

test/Interpreter/objc_protocols.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// RUN: %target-run-simple-swift
2+
// REQUIRES: executable_test
3+
// REQUIRES: objc_interop
4+
5+
import StdlibUnittest
6+
import Foundation
7+
8+
@objc protocol Horse {
9+
init()
10+
}
11+
12+
class Pony : Horse {
13+
let x = LifetimeTracked(0)
14+
15+
required init() {}
16+
}
17+
18+
var ObjCProtocolsTest = TestSuite("ObjCProtocols")
19+
20+
ObjCProtocolsTest.test("InitRequirement") {
21+
let t: Horse.Type = Pony.self
22+
23+
_ = t.init()
24+
}
25+
26+
runAllTests()

test/SILGen/objc_protocols.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,7 @@ func testInitializableExistential(_ im: Initializable.Type, i: Int) -> Initializ
267267
// CHECK: [[ARCHETYPE_META_OBJC:%[0-9]+]] = thick_to_objc_metatype [[ARCHETYPE_META]] : $@thick (@opened([[N]]) Initializable).Type to $@objc_metatype (@opened([[N]]) Initializable).Type
268268
// CHECK: [[I2_ALLOC:%[0-9]+]] = alloc_ref_dynamic [objc] [[ARCHETYPE_META_OBJC]] : $@objc_metatype (@opened([[N]]) Initializable).Type, $@opened([[N]]) Initializable
269269
// CHECK: [[INIT_WITNESS:%[0-9]+]] = witness_method [volatile] $@opened([[N]]) Initializable, #Initializable.init!initializer.1.foreign : {{.*}}, [[ARCHETYPE_META]]{{.*}} : $@convention(objc_method) <τ_0_0 where τ_0_0 : Initializable> (Int, @owned τ_0_0) -> @owned τ_0_0
270-
// CHECK: [[I2_COPY:%.*]] = copy_value [[I2_ALLOC]]
271-
// CHECK: [[I2:%[0-9]+]] = apply [[INIT_WITNESS]]<@opened([[N]]) Initializable>([[I]], [[I2_COPY]]) : $@convention(objc_method) <τ_0_0 where τ_0_0 : Initializable> (Int, @owned τ_0_0) -> @owned τ_0_0
270+
// CHECK: [[I2:%[0-9]+]] = apply [[INIT_WITNESS]]<@opened([[N]]) Initializable>([[I]], [[I2_ALLOC]]) : $@convention(objc_method) <τ_0_0 where τ_0_0 : Initializable> (Int, @owned τ_0_0) -> @owned τ_0_0
272271
// CHECK: [[I2_EXIST_CONTAINER:%[0-9]+]] = init_existential_ref [[I2]] : $@opened([[N]]) Initializable : $@opened([[N]]) Initializable, $Initializable
273272
// CHECK: store [[I2_EXIST_CONTAINER]] to [init] [[PB]] : $*Initializable
274273
// CHECK: [[READ:%.*]] = begin_access [read] [unknown] [[PB]] : $*Initializable

0 commit comments

Comments
 (0)