I have created a singleton for my app and need to know how to release it when I am through/done with it. Is there a way to do this? I have googled it and haven't found an answer yet..... Please help....... Thanks in advance!
Releasing Singleton's?
(16 posts) (7 voices)-
Posted 1 year ago #
-
Cocoa singletons usually override the release method to make sure that they are never destroyed. If you don't want that behavior, you should remove the override from your singleton class, but it kind of implies that a singleton is not really what you need.
Posted 1 year ago # -
I don't need to release a singleton in cocos2d, but for a class I created. I am making a tower defense game and this is the best way I know of doing it. It works great, except I can't figure out how to release it. So if I were to exit to a different scene and then come back to the same scene, everything is still the same.
Posted 1 year ago # -
Well, that's why I said Cocoa (Objective-C) singleton, not cocos2d. If you want it to go away when your scene goes away then perhaps it just needs to be a member of the scene instead of a singleton.
I found this article useful when I wanted to use a singleton class:
http://cocoawithlove.com/2008/11/singletons-appdelegates-and-top-level.html
Posted 1 year ago # -
Why not call a reset function rather than destroy it?
Posted 1 year ago # -
As someone already told you, if you need to release a singleton - you don't really need a singleton. And suggestion to reset your variables in singleton instead of singleton release, is also a good one...
Actually, I had once a reason to release a singleton. But it was when we wanted to pack several already finished games in one bundle, and release everything when game is finished and we wanted to go back... But that's different story. You really shouldn't need to release singleton in single project.
Posted 1 year ago # -
Ok. This is what I want to do. I have a tower defense game turret where it is it's own subclass. When I click and drag out a new turret it creates a new instance of that and then in that turret file, it determines if it can shoot or not. If it can, it will create a bullet sprite and then add it to the Layer that subclassed the turret and then the bullet will perform an action that will display on the layer not the turret. Also if I were to exit to the main menu and then go back and click on the map that has already been added as a singleton, it crashes because if you have the + (id)allocWithZone:(NSZone *)zone method, it will crash. Apple even says you can instantiate a new instance of a file that has already been "Singletonized" previously before.
If I were to do the resetting of everything in the Singleton class, where do you suggest I do so. I have tried doing the resetting in the init method but that doesn't work one bit.
I hope you guys understand what I am after, wanting. If I can figure this out, the rest of the app would be easy to make.
Edit: I got the turret class working, but I just need to be able to reset the objects in the arrays in my data class.
Posted 1 year ago # -
Just make a method in your singleton like "-(void) reset" where you will reset all your variables. And just call it whenever you want. Or I really didn't understand your problem at all :(
Posted 1 year ago # -
Thanks for all the help.... I finally figured it out, it's all good. :)
Posted 1 year ago # -
On a related note, here's a tres cool singleton macro that includes all the allocation handling, method swizzling, and even includes a "purgeSharedInstance" method in case you really do need to destroy it.
http://github.com/wiseganesha/Objective-C-Optimized-Singleton
Posted 1 year ago # -
I have a somewhat related question. How is the memory that a singleton uses not get leaked if the user were to quit your app? Furthermore, how does any objc class not leak when the app it quit? It seems so simple, yet is not haha.
Posted 1 year ago # -
When a process (app) quits, all resources used by that process are freed by the operating system.
Posted 1 year ago # -
Apple stresses memory management so you don't have app that "eats" itself. You're responsible for memory management while your app is active. Once you quit, it's not your job anymore.
And even it sounds perfect I am sure Apple is not so good with its own memory management. That's why you end up with so much unfreed memory over time. Not only on iPhone, but on Macs also...
Posted 1 year ago # -
Apple stresses memory so much bc there is so little of it! :) On the Mac ObjectiveC has gc bc (Im guessing) it has the cpu speed and memory space to accommodate the extra work and resources the gc needs.
Posted 1 year ago # -
All multitasking operating systems since CTSS in the early 60s clean up after a program exits.
The issue with memory management on iPhone/iPod/iPad is that, though the user experience feels single-tasked, the OS is multitasking, with a lot of daemons and Apple apps running in the background at all times. These daemons and programs can chew up the device's small memory in unpredictable ways, which is why Apple added memory warning hooks to iPhone apps that start to kick in at around 20MB (for early systems anyway). I've actually run a 35MB app on a 3G without it getting terminated (I got LOTS of memory warnings), but that's really playing with fire.
You absolutely MUST heed memory warnings, which usually means keeping your app's memory footprint under 20MB (some people say 24MB). If you don't, and a perfect storm brews, your app WILL be terminated.
Posted 1 year ago #
Reply
You must log in to post.