2

As mentioned in this discussion, dispatch_once is very useful in building singleton. However, could dispatch_once created instance got released and dispatch_once was not able to created that instance because it only execute once?

If so, what is the best practice to deal with it?

Community
  • 1
  • 1
Daiwei
  • 40,666
  • 3
  • 38
  • 48
  • 1
    "what is the best practice to deal with it?" Making sure your object doesn't get deallocated. – Scott Berrevoets May 30 '14 at 23:14
  • 1
    And how exactly would this singleton get released? – CrimsonChris May 30 '14 at 23:15
  • @CrimsonChris that is my first question, could singleton get released by ARC? Or it is a feature of singleton, that it will never get released? – Daiwei May 30 '14 at 23:24
  • 3
    If the reference to your singleton is static. It shouldn't ever be released unless you redirect the reference. Of course, the operating can sometimes do weird stuff in low memory situations. – CrimsonChris May 30 '14 at 23:31

1 Answers1

1

Yes.

First best practice is generally going to be to avoid the singleton in the first place.

You can also use the @synchronized(self) pattern in place of the dispatch_once pattern to guarantee only a single object exists at one time, but that it can be re-instantiated if it is ever deallocated for any reason.

You could also reset your dispatch_once variable in dealloc, I think

nhgrif
  • 61,578
  • 25
  • 134
  • 173
  • Sorry, I just updated my question with a second a part. Is there any solution to deal with it? – Daiwei May 30 '14 at 23:11
  • 2
    You could throw an exception in your singleton's dealloc method, so that if anyone accidentally releases that object, the program will crash immediately and let them know they messed up. – Catfish_Man May 30 '14 at 23:17
  • @Daiwei First best practice is generally going to be to avoid the singleton in the first place. You can also use the `@synchronized(self)` pattern in place of the `dispatch_once` pattern to guarantee only a single object exists at one time, but that it can be re-instantiated if it is ever deallocated for any reason. You could also reset your `dispatch_once` variable in `dealloc`, I think. – nhgrif May 30 '14 at 23:47
  • Thank you, if you want to include your comments in your answer, please do, as I update the question, and including comments explained the situation better. – Daiwei May 31 '14 at 00:00
  • Throwing an exception in `dealloc` is the worst idea I've heard all week. – nhgrif May 31 '14 at 00:23
  • @nhgrif Hi, why is it bad to throw exception in `dealloc` and what would be the alternative way? Thanks in advance. – Unheilig May 31 '14 at 15:29
  • @Unheilig The answer to that question isn't compact enough to fit in comments, and the question itself is opinion based so wouldn't be a good fit for StackOverflow. The alternatives are outlined in my answer. You could re-ask the question here and I'll gladly give my opinion on the matter: http://chat.stackexchange.com/rooms/12918/nschat – nhgrif May 31 '14 at 18:16