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-2500] deinit not called for singletons #45105

Open
karwa opened this issue Aug 27, 2016 · 2 comments
Open

[SR-2500] deinit not called for singletons #45105

karwa opened this issue Aug 27, 2016 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.

Comments

@karwa
Copy link
Contributor

karwa commented Aug 27, 2016

Previous ID SR-2500
Radar None
Original Reporter @karwa
Type Bug
Status Reopened
Resolution
Environment

Swift 5d21c2a

Additional Detail from JIRA
Votes 1
Component/s
Labels Bug
Assignee None
Priority Medium

md5: 87c3da070e6443586ce467201d6d37fc

Issue Description:

class A {
          static let shared = A()
          private init() {}
          deinit { print("deinit called") }
}
print("\(A.shared)")

Output:

test.A

Expected Output:

test.A
deinit called

Basically I would expect that, at the end of the program, the singleton's deinit would be called. Am I wrong to expect that?

@karwa
Copy link
Contributor Author

karwa commented Aug 27, 2016

OK, doesn't matter, but I think we should have a way to clean up global resources from singletons on application termination.

@karwa
Copy link
Contributor Author

karwa commented Nov 1, 2016

Hmm... I don't know, I'd like to reopen this to ask how to achieve a similar functionality. For example, in C++ you could write:

class SingletonTest {
private:
  SingletonTest() {}
  ~SingletonTest() {
    std::cout << "Destructing!!" << std::endl;
  }

public:
  static SingletonTest& get_instance()  {
    static SingletonTest instance;
    return instance;
  }
};

To create a lazily-initialised singleton that will be destructed on exit; but we can't have static locals in Swift. So how would I create a singleton which has some external resources (e.g. temporary files) and cleans them up properly on exit?

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 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.
Projects
None yet
Development

No branches or pull requests

1 participant