Skip to content

[Swift 2.2.1][Swift runtime] Deallocation of partial class instances for Objective-C-derived classes #1798

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/swift/Runtime/ObjCBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ extern "C" id objc_initWeak(id*, id);
extern "C" id objc_storeWeak(id*, id);
extern "C" void objc_destroyWeak(id*);
extern "C" id objc_loadWeakRetained(id*);
extern "C" Class object_setClass(id, Class);

// Description of an Objective-C image.
// __DATA,__objc_imageinfo stores one of these.
Expand Down
44 changes: 38 additions & 6 deletions stdlib/public/runtime/HeapObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -453,16 +453,48 @@ extern "C" void swift_deallocPartialClassInstance(HeapObject *object,
return;

// Destroy ivars
auto *objectMetadata = _swift_getClassOfAllocated(object);
while (objectMetadata != metadata) {
auto classMetadata = objectMetadata->getClassObject();
assert(classMetadata && "Not a class?");
auto *classMetadata = _swift_getClassOfAllocated(object)->getClassObject();
assert(classMetadata && "Not a class?");
while (classMetadata != metadata) {
#if SWIFT_OBJC_INTEROP
// If we have hit a pure Objective-C class, we won't see another ivar
// destroyer.
if (classMetadata->isPureObjC()) {
// Set the class to the pure Objective-C superclass, so that when dealloc
// runs, it starts at that superclass.
object_setClass((id)object, (Class)classMetadata);

// Release the object.
objc_release((id)object);
return;
}
#endif

if (auto fn = classMetadata->getIVarDestroyer())
fn(object);
objectMetadata = classMetadata->SuperClass;
assert(objectMetadata && "Given metatype not a superclass of object type?");

classMetadata = classMetadata->SuperClass->getClassObject();
assert(classMetadata && "Given metatype not a superclass of object type?");
}

#if SWIFT_OBJC_INTEROP
// If this class doesn't use Swift-native reference counting, use
// objc_release instead.
if (!usesNativeSwiftReferenceCounting(classMetadata)) {
// Find the pure Objective-C superclass.
while (!classMetadata->isPureObjC())
classMetadata = classMetadata->SuperClass->getClassObject();

// Set the class to the pure Objective-C superclass, so that when dealloc
// runs, it starts at that superclass.
object_setClass((id)object, (Class)classMetadata);

// Release the object.
objc_release((id)object);
return;
}
#endif

// The strong reference count should be +1 -- tear down the object
bool shouldDeallocate = object->refCount.decrementShouldDeallocate();
assert(shouldDeallocate);
Expand Down
89 changes: 89 additions & 0 deletions test/Interpreter/SDK/objc_failable_initializers.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// RUN: %target-run-simple-swift
// REQUIRES: executable_test
// REQUIRES: objc_interop

import StdlibUnittest
import Foundation

var ObjCFailableInitTestSuite = TestSuite("ObjCFailableInit")

class Canary {
static var count: Int = 0

init() {
Canary.count += 1
}

deinit {
Canary.count -= 1
}
}

extension NSDate {
convenience init?(b: Bool) {
guard b else { return nil }
self.init()
}
}

class MyDate : NSDate {
var derivedCanary = Canary()

static var count = 0

override init() {
MyDate.count += 1
super.init()
}

required convenience init(coder: NSCoder) {
fatalError("not implemented")
}

deinit {
MyDate.count -= 1
}

@objc convenience init?(b: Bool) {
guard b else { return nil }
self.init()
}
}

class MyDerivedDate : MyDate {
var canary = Canary()

static var derivedCount = 0

override init() {
MyDerivedDate.count += 1
}

deinit {
MyDerivedDate.count -= 1
}

}

func mustFail<T>(f: () -> T?) {
if f() != nil {
preconditionFailure("Didn't fail")
}
}

ObjCFailableInitTestSuite.test("InitFailure_Before") {
mustFail { NSDate(b: false) }
expectEqual(0, Canary.count)

mustFail { MyDate(b: false) }
expectEqual(0, Canary.count)
expectEqual(0, MyDate.count)

mustFail { MyDerivedDate(b: false) }
expectEqual(0, Canary.count)
expectEqual(0, MyDate.count)
expectEqual(0, MyDerivedDate.derivedCount)
}

runAllTests()