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-7777] Memory leak when storing closure with optional numeric type #50316

Closed
swift-ci opened this issue May 25, 2018 · 8 comments
Closed

[SR-7777] Memory leak when storing closure with optional numeric type #50316

swift-ci opened this issue May 25, 2018 · 8 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself memory leak bug: Memory leak

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-7777
Radar rdar://problem/40558163
Original Reporter damiandudycz (JIRA User)
Type Bug
Status Resolved
Resolution Done

Attachment: Download

Environment

iOS 11.3, Xcode 9.3.1 (9E501), Swift 4.1

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

md5: f41a6aba51fd2884fae483d23fedf5d2

Issue Description:

If you create an array of closures that have optional numeric type in parameters (eq. Double?), then adding new closures to this array causes a leak. Only if you also capture self in this closure. Very simple example below:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    
    var observations = [(Double?) -> Void]()

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        observations.append { [unowned self] (new) in
            print(self)
        }
        
        return true
    }

}
@belkadan
Copy link
Contributor

Sorry, can you share more information about what the leak is?

@swift-ci
Copy link
Collaborator Author

Comment by Damian Dudycz (JIRA)

Profiler shows Malloc 16 bytes and 32 bytes with responsible library libswiftCore.dylib. One shows responsible frame: swift_unknownUnownedInit, second is: swift_slowAlloc. In memory graph I see leaked "Swift closure context". Screenshots are here: https://i.stack.imgur.com/TG8tT.png https://i.stack.imgur.com/03n1E.png . All you need to reproduce this error is create new single view application for iOS and paste code to AppDelegate. After running there is a leak. This happens in device - I tested on iPhone 6s, haven't seen this in simulator.

@belkadan
Copy link
Contributor

Thanks! I thought you were talking about the AppDelegate being leaked at first, which confused me. The note about device-only is super useful too.

@swift-ci create

@belkadan
Copy link
Contributor

(It's possible this is already fixed in the 4.2 development snapshots too.)

@eeckstein
Copy link
Member

I could not reproduce the leak. Can you attach the whole project?
And/or try with a recent 4.2 development snapshot and see if the problem is fixed?

@swift-ci
Copy link
Collaborator Author

Comment by Damian Dudycz (JIRA)

Sure. Here is the project - https://www.dropbox.com/s/745j5hclhtncj22/Test.zip?dl=1
And here a video showing this leak - https://www.dropbox.com/s/ln509tw53r9vktd/iOS_Leak.mov?dl=1
I will try this on 4.2 and let know if this helped.
Please note, that I was testing this on device - iPhone 6s. This is not happening in the simulator.

@swift-ci
Copy link
Collaborator Author

swift-ci commented Jun 1, 2018

Comment by Damian Dudycz (JIRA)

Just to confirm, this is fixed in Swift 4.2. Thanks

@eeckstein
Copy link
Member

great, thanks for checking!

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
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 memory leak bug: Memory leak
Projects
None yet
Development

No branches or pull requests

3 participants