Note that there are some explanatory texts on larger screens.

plurals
  1. PORegarding memory management in Objective C
    primarykey
    data
    text
    <p>According to the static analyzer if we have the following property:</p> <pre><code>@property (retain, nonatomic) SomeObject * object; </code></pre> <p>and then we assign the property like so: </p> <pre><code>self.object = [SomeObject alloc] init]; </code></pre> <p>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:</p> <pre><code>self.object = [[SomeObject alloc] init] autorelease]; </code></pre> <p>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?</p> <p>EDIT: I have a concrete example now of the issue I was running into.</p> <pre><code> 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 &gt;= 1 &amp;&amp; [[contextID objectAtIndex:contextID.count - 1] isEqual:[NSNull null]]) &amp;&amp; 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; </code></pre> <p>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]).</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload