0

I was wondering how you would release a singleton

+ (DSActivityView *)activityViewForView:(UIView *)addToView withLabel:(NSString *)labelText width:(NSUInteger)labelWidth;
 {
    // Not autoreleased, as it is basically a singleton:
    return [[self alloc] initForView:addToView withLabel:labelText width:labelWidth];
 }

When analysing this using the analyse tool i get the following error : Potential leak of object on line 90. which is the line that returns.

I have tried autorelease that solves the error message problem but im not convinced its the right solution since i read that autoreleasing singletons is not good. Would someone be able to assist me in identifying how best to release this object?

Thanks

Piyush Dubey
  • 2,416
  • 1
  • 24
  • 39
CodeGeek123
  • 4,341
  • 8
  • 50
  • 79

3 Answers3

2

The reason why the analyzer gives you the warning is, basically, the method name:

+ (DSActivityView *)activityViewForView:(UIView *)addToView withLabel:(NSString *)labelText width:(NSUInteger)labelWidth;

according to Objective-C conventions, all method names starting with "create"/"new"/... return a retained object; your method falls under the category of convenience constructors, which are expected to return autoreleased objects, hence the warning.

On the other hand, you say this is a singleton, but in fact it is not. So, you could possibly end up calling this method more than once and thus have an actual leak. A basic way to make your method safer (and more singleton-like) is:

+ (DSActivityView *)activityViewForView:(UIView *)addToView withLabel:(NSString *)labelText width:(NSUInteger)labelWidth;
{
    static DSActivityView* gDSActivityViewSingleton = nil;
    if (!gDSActivityViewSingleton)
          gDSActivityViewSingleton = [[self alloc] initForView:addToView withLabel:labelText width:labelWidth];
    return gDSActivityViewSingleton;
}

This would both make the analyzer relax and give you more safety in front of the possibility of misuse of the method.

sergio
  • 68,819
  • 11
  • 102
  • 123
1

Use autorelease. There's no reason not to. Basically ownership of the object belongs to the object, so you're never going to be able to manually release it. As its a singleton it doesn't matter if you don't own it because presumably next time you call it and need it in scope you'll use another convenience method and it will get instantiated again.

If you want to have ownership of the object then you will need to instantiate it as normal and then you will be able to retain and release it.

Also, read sergio's edit about it not being a "proper" singleton. :p

Also, if you can, convert to ARC and you won't have to worry about this!

Thomas Clayson
  • 29,657
  • 26
  • 147
  • 224
  • Unfortunately the project needs to work on older devices too starting from 4 so i cannot use ARC :( – CodeGeek123 Sep 03 '12 at 16:17
  • arc is supported on iOS 4 and above :) http://stackoverflow.com/questions/7747783/is-arc-really-supported-in-ios-4-the-ios-4-2-sdk-is-missing-arc-related-symbols - basically just don't use `weak` references. – Thomas Clayson Sep 03 '12 at 16:18
1

U are doing it wrong. Consider:

If you calling activityViewForView multiple times, you won't get get the same object over and over again. It only would initialize a new object and give you the pointer to it!!!

To make this thing a singleton, you have to store the created object in a constant variable and make sure, you have a reference to this object all the time your app is running (for instance declare your pointer to this object in appDelegate).

Then every time you call activityViewForView you have to check the constant variable if it is pointing to a valid object. If so, return the valid object, if not, create it and store it in your constant static variable (creation is done only once).

If you do use ARC you're all set. If not, release your object (use dealloc method)

Fab1n
  • 2,103
  • 18
  • 32