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-10531] Observer added in AppDelegate is de-initialized immediately if not stored anywhere else. #3430

Closed
swift-ci opened this issue Apr 22, 2019 · 3 comments

Comments

@swift-ci
Copy link
Contributor

Previous ID SR-10531
Radar None
Original Reporter Stephan Sann (JIRA User)
Type Bug
Status Resolved
Resolution Invalid
Environment

macOS: 10.14.4

Xcode: Version 10.2.1 (10E1001)

Swift 5

Additional Detail from JIRA
Votes 0
Component/s Foundation, Standard Library
Labels Bug
Assignee None
Priority Medium

md5: fa76162c2848920848aec995aa6bf1c2

Issue Description:

I created a proxy class for my SKPaymentTransactionObserver. It takes the real SKPaymentTransactionObserver with its initializer.

Now within the AppDelegate I added a new instance of my proxy to the SKPaymentQueue:

SKPaymentQueue.default().add(LoggingSKPaymentTransactionObserver(aSKPaymentTransactionObserver: CsConstants.STORE_KIT_AGENT))

I found that the proxy is de-initialized immediately after initialization.
To make it last I have to store it somewhere else, so it will work as expected.

In my opinion a bug, since an object which is set as an observer should not be deallocated.

@belkadan
Copy link

This isn't anything to do with Swift, just Cocoa, but it's also correct behavior there. If the SKPaymentQueue kept the observer alive, it would never know when to deallocate it. (This pattern is extremely common throughout Cocoa, though in most cases it's because the observer owns the observed object, and you'd get a strong reference cycle and therefore a leak if the observed kept the observer alive.)

@swift-ci
Copy link
Contributor Author

Comment by Stephan Sann (JIRA)

Thank you very much for the clarification @belkadan .
Also sorry if I came to the wrong place.

Anyhow - and maybe I am spoiled by the Java Garbage Collector and Swift can't do this - IMHO the right time to deallocate the observer would be when I remove it from the SKPaymentQueue via "func remove(_ observer: SKPaymentTransactionObserver)" and there are no more other references to the object.

Until then I find it spooky an observer I added just doesn't work without any evidence what happened.
These are the things one spends hours finding out about 🙁

@belkadan
Copy link

Ah, but you can't do that removal if you don't have a reference to it from elsewhere!

You're right that garbage collection would handle this case; cycle collection is the main thing garbage collection can do that plain reference counting cannot. It's a tradeoff for sure.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 5, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants