Skip to content
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

[SR-815] Generic classes with default property initializers bug #43427

Closed
swift-ci opened this issue Feb 25, 2016 · 25 comments
Closed

[SR-815] Generic classes with default property initializers bug #43427

swift-ci opened this issue Feb 25, 2016 · 25 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself crash Bug: A crash, i.e., an abnormal termination of software run-time crash Bug → crash: Swift code crashed during execution

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-815
Radar rdar://problem/25318716
Original Reporter wiruzx (JIRA User)
Type Bug
Environment

Swift 2.1.1, iOS 7
Swift 2.2, iOS 9.3

Additional Detail from JIRA
Votes 22
Component/s Compiler
Labels Bug, RunTimeCrash
Assignee None
Priority Medium

md5: ab6cd5d5f6905bbd52b11768623b6abf

is duplicated by:

  • SR-1659 Swift2.2.1 generics property crash

relates to:

  • SR-1201 All generic class crash on iOS 7

Issue Description:

class Test<T> {
    let object = NSObject()
    var value: T?
    
    func test() {
        print(object) // EXC_BAD_ACCESS here
    }
}

let x = Test<Int>()
x.test()

It doesn't matter if object will be NSObject or Swift's ref type.

Bug is not being reproduced if you either:

  • make class non-generic,

  • remove `value` property,

  • swap `object` and `value` properties declarations,

  • initialize object in `init`

This issue reproduces only on iOS 7

@belkadan
Copy link
Contributor

Sorry, what's crashing? Can you include the code that constructs one of these things and calls test?

@swift-ci
Copy link
Collaborator Author

Comment by Victor Shamanov (JIRA)

@belkadan Just create an instance of Test class parameterized by any type and call the test() method. I've added it to issue description

@swift-ci
Copy link
Collaborator Author

Comment by Miroslav Kuzmisin (JIRA)

same problem here, please note that this bug has to be prioritized as urgent/critical, because app for ios7 cannot be published

problem comes with latest Xcode(7.3 - 7D175)/swift

Apple Swift version 2.2 (swiftlang-703.0.18.1 clang-703.0.29)
Target: x86_64-apple-macosx10.9

you have to run it on real device with ios7 (ios8, ios9 work without problem), this bug is 100% reproducible, just run this code

class SomeClass<T> {

    var anything: Int?

    // var generic: T? // crash with or without
}

let test = SomeClass<String>() // EXC_BAD_ACCES

please let us know, if you can reproduce it, or you will need another steps from us, many thanks ...

@swift-ci
Copy link
Collaborator Author

Comment by Victor Shamanov (JIRA)

kuzmisin (JIRA User) Please notice, that you can work around it just by changing declaration order of your properties.
This should work:

class SomeClass<T> {
    var generic: T?
    var anything: Int?
}

let test = SomeClass<String>()

@swift-ci
Copy link
Collaborator Author

Comment by Miroslav Kuzmisin (JIRA)

Victor thanks ... hmm, for simple case, like

class SomeClass<T> {
    var generic: T?
    var dummy: Int?
}

works, but for real example, still crashing

class SomeClass<T> {
    var generic: [Int: T]?
    var dummy: Int?
}

@swift-ci
Copy link
Collaborator Author

Comment by Victor Shamanov (JIRA)

kuzmisin (JIRA User) Did you try to initialize those properties in initializer?

class SomeClass<T> {
   var generic: [Int: T]?
   var dummy: Int?
   init() {
        generic = nil
        dummy = nil
   }
}

@swift-ci
Copy link
Collaborator Author

Comment by Miroslav Kuzmisin (JIRA)

yes, and also with

generic = [Int: T]()
dummy = 1

still crashing (note: I am testing it on clear iOS project to avoid any 3rd party problems)

@swift-ci
Copy link
Collaborator Author

Comment by Victor Shamanov (JIRA)

kuzmisin (JIRA User) maybe this would work:

class SomeClass<T> {

    private var _generic: Any?

    var generic: [Int: T]? {
        get {
            return _generic as! [Int: T]?
        }
        set {
            _generic = newValue
        }
    }

    var dummy: Int?
}

@swift-ci
Copy link
Collaborator Author

Comment by Miroslav Kuzmisin (JIRA)

wiruzx (JIRA User) nope, still crashing ... maybe another user with same problem http://stackoverflow.com/questions/36201272/tests-with-generic-classes-crash-since-swift-2-2

@swift-ci
Copy link
Collaborator Author

Comment by Daniel Klöck (JIRA)

I seem to have the same problem, but I'm using swift 2.2 and it also happens on a device with Version 9.3 (13E233) and on the simulator (also with 9.3, I haven't tested older versions). Is there any other solution yet? Half of my classes are affected. This should be high prio.

In my case, the problem began with swift 2.2, before that update everything was working fine.

@swift-ci
Copy link
Collaborator Author

Comment by Daniel Klöck (JIRA)

This guy also seems to be having the same issue: http://stackoverflow.com/questions/36235369/xcode-7-3-generics

@swift-ci
Copy link
Collaborator Author

Comment by Daniel Klöck (JIRA)

I corrected my problem by initializing all object in `init` as the description suggested

@swift-ci
Copy link
Collaborator Author

swift-ci commented Apr 6, 2016

Comment by WenLong Meng (JIRA)

Environment: swift 2.2
iOS 7

class Object1 {
  init () {
    let object2 = Object2(self)
  }
}

class Object2<T: AnyObject> {
  weak var object: T?

  init(_ object: T) {
    self.object = object
  }
}
-   thread \#1: tid = 0x8890, 0x0030095e libswiftCore.dylib\`swift_initClassMetadata_UniversalStrategy + 448, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=2, address=0xf3e10)  
    frame \#0: 0x0030095e libswiftCore.dylib\`swift_initClassMetadata_UniversalStrategy + 448  
    frame \#1: 0x000ef6dc BugTest\`\_\_\_lldb_unnamed_function4$$BugTest + 164  
    frame \#2: 0x00301d1c libswiftCore.dylib\`(anonymous namespace)::GenericCacheEntry\* llvm::function_ref\<(anonymous namespace)::GenericCacheEntry\* ()\>::callback_fn\<swift_getGenericMetadata::$\_1\>(long) + 18  
    frame \#3: 0x00301c0e libswiftCore.dylib\`swift::MetadataCache\<(anonymous namespace)::GenericCacheEntry\>::addMetadataEntry(swift::EntryRef\<(anonymous namespace)::GenericCacheEntry\>, ConcurrentList\<swift::MetadataCache\<(anonymous namespace)::GenericCacheEntry\>::EntryPair\>&, llvm::function_ref\<(anonymous namespace)::GenericCacheEntry\* ()\>) + 80  
    frame \#4: 0x002ffb28 libswiftCore.dylib\`swift::MetadataCache\<(anonymous namespace)::GenericCacheEntry\>::findOrAdd(void const\* const\*, unsigned long, llvm::function_ref\<(anonymous namespace)::GenericCacheEntry\* ()\>) + 318  
    frame \#5: 0x002ffbca libswiftCore.dylib\`swift_getGenericMetadata1 + 64

-   frame \#6: 0x000ef734 BugTest\`type metadata accessor for Object2\<Object1\> + 76 at Object1.swift:0  
    frame \#7: 0x000ef108 BugTest\`Object1.init() -\> Object1 + 24 at Object1.swift:13  
    frame \#8: 0x000ef16c BugTest\`Object1.\_\_allocating_init() -\> Object1 + 36 at Object1.swift:0  
    frame \#9: 0x000ee9cc BugTest\`ViewController.viewDidLoad(self=0x14d5c4d0) -\> () + 164 at ViewController.swift:15  
    frame \#10: 0x000eea84 BugTest\`@objc ViewController.viewDidLoad() -\> () + 56 at ViewController.swift:0  
    frame \#11: 0x30b38a52 UIKit\`-\[UIViewController loadViewIfRequired\] + 518  
    frame \#12: 0x30b38810 UIKit\`-\[UIViewController view\] + 24  
    frame \#13: 0x30b3f488 UIKit\`-\[UIWindow addRootViewControllerViewIfPossible\] + 64  
    frame \#14: 0x30b3cdd8 UIKit\`-\[UIWindow \_setHidden:forced:\] + 304  
    frame \#15: 0x30ba6a50 UIKit\`-\[UIWindow makeKeyAndVisible\] + 60  
    frame \#16: 0x30ba36e4 UIKit\`-\[UIApplication \_callInitializationDelegatesForURL:payload:suspended:\] + 1820  
    frame \#17: 0x30b9dcc8 UIKit\`-\[UIApplication \_runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:\] + 720  
    frame \#18: 0x30b39c96 UIKit\`-\[UIApplication handleEvent:withNewEvent:\] + 3550  
    frame \#19: 0x30b38df8 UIKit\`-\[UIApplication sendEvent:\] + 72  
    frame \#20: 0x30b9d404 UIKit\`\_UIApplicationHandleEvent + 616  
    frame \#21: 0x331a6b54 GraphicsServices\`\_PurpleEventCallback + 608  
    frame \#22: 0x331a673e GraphicsServices\`PurpleEventCallback + 34  
    frame \#23: 0x2e2cf83e CoreFoundation\`**CFRUNLOOP_IS_CALLING_OUT_TO_A\_SOURCE1_PERFORM_FUNCTION** + 34  
    frame \#24: 0x2e2cf7da CoreFoundation\`\_\_CFRunLoopDoSource1 + 346  
    frame \#25: 0x2e2cdfa6 CoreFoundation\`\_\_CFRunLoopRun + 1406  
    frame \#26: 0x2e2387a8 CoreFoundation\`CFRunLoopRunSpecific + 524  
    frame \#27: 0x2e23858a CoreFoundation\`CFRunLoopRunInMode + 106  
    frame \#28: 0x30b9c62a UIKit\`-\[UIApplication \_run\] + 762  
    frame \#29: 0x30b97890 UIKit\`UIApplicationMain + 1136  
    frame \#30: 0x000f0158 BugTest\`main + 172 at AppDelegate.swift:12  
    frame \#31: 0x38f97ab6 libdyld.dylib\`start + 2

@belkadan
Copy link
Contributor

belkadan commented Apr 6, 2016

Victor, didn't you say your original problem appeared with 2.1 as well? That's how I interpreted your "Environment" entry.

@swift-ci
Copy link
Collaborator Author

swift-ci commented Apr 6, 2016

Comment by Victor Shamanov (JIRA)

@belkadan Couldn't tell if it was in 2.1, but it surely was in 2.1.1

@belkadan
Copy link
Contributor

belkadan commented Apr 6, 2016

Okay, then it sounds like Daniel and Miroslav are hitting a different issue. (It could certainly be the same underlying issue, but I don't think we can assume that.) Maybe we should track that with a different bug, and link them?

@swift-ci
Copy link
Collaborator Author

swift-ci commented Apr 8, 2016

Comment by Victor Shamanov (JIRA)

Make sense.
dk (JIRA User), kuzmisin (JIRA User), could you please create tickets describing your bugs and I'll link them?

@swift-ci
Copy link
Collaborator Author

Comment by Miroslav Kuzmisin (JIRA)

wiruzx (JIRA User) ok, no problem, I've created BUG [SR-1201]

@swift-ci
Copy link
Collaborator Author

swift-ci commented May 4, 2016

Comment by Miroslav Kuzmisin (JIRA)

looks like fixed in (today updated) XCODE Version 7.3.1 (7D1014), in short test everything is working - no crash

@acecilia
Copy link

acecilia commented May 7, 2016

I also confirm that looks like fixed in xcode 7.3.1

@swift-ci
Copy link
Collaborator Author

swift-ci commented May 7, 2016

Comment by Egor (JIRA)

Still not working for subclasses of generic classes with attributes

class GenericClass<T> {
    let attribute = 0
}

class GenericSubclass: GenericClass<Int> {
    var otherAttribute: Int { return 0 }
}

let _ = GenericSubclass()
class GenericClass<T> {
    let attribute = 0
}

class GenericSubclass: GenericClass<Int> {
    let otherAttribute = 0
}

let _ = GenericSubclass()
class GenericClass<T> {
    var value: T?
    let attribute = 0
}

class GenericSubclass: GenericClass<Int> {
    let otherAttribute = 0
}

let _ = GenericSubclass()

@slavapestov
Copy link
Member

Egor, what version of XCode are you using?

@swift-ci
Copy link
Collaborator Author

Comment by Egor (JIRA)

Slava, XCode 7.1.3, device - iPhone 4, iOS 7.1.2

@swift-ci
Copy link
Collaborator Author

Comment by Ryan (JIRA)

Hi,

It would be greatly appreciated if this bug were resolved as it makes Swift and iOS7 a almost no-go. Thank you for your consideration! 🙂

A work-around was discovered though that would at least let us support iOS7 without a significant rewrite.
It looks like all subclasses must also be generic so that the parent generic works correctly in iOS7.
The subclass's generic type does not matter and it does not need to be the parent's generic type.

Example:

// This heavily pollutes the codebase, so let's keep track of it but using a common meaningless generic value so we can find and destroy later when we no longer support iOS7
protocol iOS7SwiftGenericFixProtocol {}
struct iOS7SwiftGenericFixType: iOS7SwiftGenericFixProtocol {}

class GenericClass<T> {
    var value: T?
    let attribute = 0
}

class GenericSubclass<Element: iOS7SwiftGenericFixProtocol>: GenericClass<Int> {
    let otherAttribute = 0
}

let _ = GenericSubclass<iOS7SwiftGenericFixType>()

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis
Copy link
Collaborator

Unfortunately, iOS 7 appears to be out of reach for the latest Swift release. You are welcome to reopen the issue if it happens to reemerge on a less distant deployment target.

@AnthonyLatsis AnthonyLatsis closed this as not planned Won't fix, can't repro, duplicate, stale Jul 16, 2022
@AnthonyLatsis AnthonyLatsis added the crash Bug: A crash, i.e., an abnormal termination of software label Dec 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself crash Bug: A crash, i.e., an abnormal termination of software run-time crash Bug → crash: Swift code crashed during execution
Projects
None yet
Development

No branches or pull requests

5 participants