Im pretty new to objective-c programming and releasing of objects is my greatest headache. I'm always doubting on what need to be released, and my times I've end up releasing the wrong variable and getting a BAD EXEC crash. I've read apple's guide on memory management, but I cant always go from their examples to my code.
One of these situations is my singletons (Im a big Singleton guy).
I have one define as this:
static Configuration* _instance;
+(Configuration*)getInstance{
if (_instance == NULL){
_instance = [Configuration alloc];
[_instance initConfig];
}
return _instance;
}
In my code I use it like this:
//Store configuration
Configuration* conf = [Configuration getInstance];
conf.userName = self.userName.text;
conf.cellPhone = self.phoneNumber.text;
Do I need to release the "conf" variable?
When should I release the _instance?
Since Im running this code on iPhone, what happens with the vars I don't release? will they affect the iPhone performance?
Then you can reference the singleton from anywhere by calling the following function: MyManager *sharedManager = [MyManager sharedManager]; I've used this extensively throughout my code for things such as creating a singleton to handle CoreLocation or CoreData functions.
A singleton should be used when managing access to a resource which is shared by the entire application, and it would be destructive to potentially have multiple instances of the same class. Making sure that access to shared resources thread safe is one very good example of where this kind of pattern can be vital.
The Singleton's purpose is to control object creation, limiting the number to one but allowing the flexibility to create more objects if the situation changes. Since there is only one Singleton instance, any instance fields of a Singleton will occur only once per class, just like static fields.
The advantage of Singleton over global variables is that you are absolutely sure of the number of instances when you use Singleton, and, you can change your mind and manage any number of instances.
When you created the Configuration instance with "_instance = [Configuration alloc]; [_instance initConfig];" it had a retain count of one from the alloc call. If you were to release conf after "conf.cellPhone = self.phoneNumber.text;" then it would be deallocated at that point.
When you first create an object with alloc, or copy, or mutableCopy it will have a retain count of 1. Each call to retain increases that retain count by one. Each call to release decreases that retain count by 1. Calling autorelease just means "Call release for me later", so if retains and releases are like checks, autorelease is like future dating a check.
Your code that accesses the Configuration singleton does not retain it, copy it, or mutable copy it, so it should not release it.
As your code is written now, the Configuration object will never be released, and will live for then entirety of the applications life, which is typically what you want with a singleton.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With