Take the 2-minute tour ×

In a Utilities class, I have the following method:

+ (Division *) getNationalDivision
    Division *defaultDivision = [[[Division alloc] init] autorelease];
    defaultDivision.Id = 0;
    defaultDivision.name = @"National";

    return defaultDivision;

I have a division allocted in my app delegate to store the division throughout the app, so in one of my view controllers I have:

appDel.currentDivision = [[Utilities getNationalDivision] retain];

In the app delegate .h I have:

@property (nonatomic, retain) Division *currentDivision;

In the app delegate .m I have:

currentDivision = [[Division alloc] init];

When I analyze, I get potential leak of an object that points to the above line. Any ideas? If I dont retain the national division, it doesnt work. Also, just to note, everything works fine. I just want to make sure I am not leaking something.

share|improve this question
How did you declare currentDivision? –  blueberryfields Jan 25 '12 at 17:32
You may want to mention what your currentDivision property is defined as. I presume retain? In that case, the extra retain here is very likely an over-retain. We'll know more with the @property definition. –  Ryan Wersal Jan 25 '12 at 17:32
I added the code, I am allocating it –  Jesse Jan 25 '12 at 17:51
add comment

2 Answers

up vote 1 down vote accepted

Check how you declare the property currentDivision in your app delegate. If it is assign (which I suppose, since you are retaining it before assigning through the property) then the original value you assigned to it:

 currentDivision = [[Division alloc] init];

will not get released when you execute:

 appDel.currentDivision = [[Utilities getNationalDivision] retain];

hence, the object you created in the app delegate will leak. Do a release manually and check whether the analyzer keeps complaining:

[app.currentDivision release];

This only makes sense in case your property is declared as assign.

In case it is declared as retain, then the fault is on line:

 appDel.currentDivision = [[Utilities getNationalDivision] retain];

where you should not do the retain manually.

share|improve this answer
add comment

The retain is done by the synthesized property accessor method. Remove it.

share|improve this answer
+1. But how many times do we have to keep saying this? –  Abizern Jan 25 '12 at 17:34
He says it doesn't work if he removes retain. So it probably isn't being retained as a property... @Jesse: please provide more info on how you define currentDivision property (or is it a local variable...) –  rokjarc Jan 25 '12 at 17:43
More info added. –  Jesse Jan 25 '12 at 17:53
and if you use [appDel setCurrentDivision:[Utilities getNationalDivision]];? –  rokjarc Jan 25 '12 at 17:55
add comment

Your Answer


