0
votes

According to the static analyzer if we have the following property:

@property (retain, nonatomic) SomeObject * object;

and then we assign the property like so:

self.object = [SomeObject alloc] init];

a leak occurs. This makes sense because the alloc init adds +1 to the retain count and then the retaining property also increments the retain count. What is the best solution here? typically I just add an autorelease like so:

self.object = [[SomeObject alloc] init] autorelease];

But sometimes this creates problems for me and I end up over releasing the object causing my app to crash. I don't have any specific examples right now but I remember I had to take out some autoreleases cause of the application crashing. Is there something I am missing here?

EDIT: I have a concrete example now of the issue I was running into.

    NSMutableArray *newData = [NSMutableArray array];

    //If this is true then we are showing all of the items in that level of hierarchy and do not need to show the summary button.
    if (!(contextID.count >= 1 && [[contextID objectAtIndex:contextID.count - 1] isEqual:[NSNull null]]) && contextID.count != 0)
    {
        GeographyPickerItem * firstItem = [[GeographyPickerItem alloc] init];

        firstItem.primaryString = [NSString stringWithString:@"Summary"];
        firstItem.subString = [NSString stringWithString:@""];
        firstItem.isSummaryItem = YES;

        [newData addObject:firstItem];
        [firstItem release]; //TODO: Figure out why this is causing EXC_BAD_ACCESS errors
    }

    self.hierData = newData;

The code above is in the init method of a viewcontroller. HierData is a retained property, which is released in the viewControllers dealloc method. GeographyPickerItem retains the two strings, primaryString and subString and releases them in its own dealloc method. My application crashes (sometimes) when the viewControllers are de-alloced following a pop off of a navigation controller. It crashes with a EXC_BAD_ACCESS signal in the dealloc method of GeographyPickerItem (either on [substring release] or [primaryString release]).

I don't understand why this is happening because I believe I am following proper memory management guidelines. If I comment out firstItem release everything is fine.

4

4 Answers

4
votes

The autorelease method you mention is fine, as is the other common idiom of:

SomeObject *thing = [[SomeObject alloc] init];
self.object = thing;
[thing release];

If you end up overreleasing later on, that is your problem. This part, which you're apparently doing correctly, is not the problem.

1
votes
SomeObject * new_object = [SomeObject alloc] init];
self.object = new_object;
[new_object  release];

or use ARC

0
votes

check the GeographyPickerItem, if the strings properties are assign (and change to retain), or check if you always initialize them (before release).

also remember the difference of manually allocating :

[[NSString alloc] initWith...]

You must release or autorelease.

[NSString stringWith...] 

No need to release.

or use ARC like meggar said

0
votes

Turns out the issue was simple, my dealloc method called super dealloc at the start of the method rather than at the end. You always have to release your instance variables before you call [super dealloc]!