Note that there are some explanatory texts on larger screens.

plurals
  1. POPesky leak in setter/getter methods
    text
    copied!<p>It seems like I keep asking the same questions, memory related. My current code works exactly as I intend it, but I cannot figure why I am showing a leak here in Instruments.</p> <pre><code>-(NSDate *)startTimeAndDate { NSDate *dateToReturn = nil; if (startTimeAndDate != nil) { dateToReturn = [startTimeAndDate retain]; } else { //is currently nil, this will be the initial setting //return default time if we have a working date if (finishTimeAndDate != nil) { dateToReturn = [[self dateFromDate:finishTimeAndDate withNewTime:defaultStartTime]retain]; } else { //return the default time with today's date if we have nothing set as yet dateToReturn = [[self dateFromDate:[NSDate date] withNewTime:defaultStartTime] retain]; } //save the initial setting self.initialStartDateAndTime = [[dateToReturn copy] autorelease]; } [startTimeAndDate release]; startTimeAndDate = dateToReturn; return startTimeAndDate; } -(void)setStartTimeAndDate:(NSDate *)inStartTimeAndDate { BOOL initialAssignment = NO; if (startTimeAndDate == nil) { initialAssignment = YES; } if (startTimeAndDate != inStartTimeAndDate) { //skip everything if passed object is same as current //check that the start time is prior to finish only if finish time has been entered NSDate *dateToSetStartTo = nil; if (finishTimeAndDate != nil) { if ([inStartTimeAndDate earlierDate:finishTimeAndDate] == inStartTimeAndDate) { // use the new time, it is earlier than current finish time dateToSetStartTo = [inStartTimeAndDate retain]; } else { //start time is not earlier then finish time // the received entry is invalid, set start time to 1 default interval from finish dateToSetStartTo = [[finishTimeAndDate dateByAddingTimeInterval:-self.defaultTimeInterval] retain]; } } else { //finish time is nil // use the new time without testing, nothing else is set dateToSetStartTo = [inStartTimeAndDate retain]; } [startTimeAndDate release]; startTimeAndDate = dateToSetStartTo; } if (initialAssignment) { self.initialStartDateAndTime = [[self.startTimeAndDate copy] autorelease]; } } </code></pre> <p>So far as I can see, I am balancing all retains with <code>release</code> or <code>autorelease</code>. The leak appears to be caused on the first pass only. I have a view controller, it creates my model (wherein this code lies) and sets a start date, nothing else is done at that point. If I close that view controller at that point, Instruments shows that I am leaving the date object as a leak.</p> <p>I placed a <code>NSLog</code> to show retain count at <code>dealloc</code> and, sure enough, it has retain count of 2 before my final release is called, leaving a retain count of 1 when it should have been destroyed. It is always the same regardless if I close immediately after initialization or set and get a hundred times. <code>retainCount</code> is 2 prior to my final call to <code>release</code> in <code>dealloc</code>.</p> <p>I have been looking at this all weekend and cannot figure where I've gone wrong.</p> <p>To clarify, the initial call is to set the <code>startTimeAndDate</code> property. At that point all other fields are nil or 0 if not objects. That <code>startTimeAndDate</code> object appears to be the leaking object.</p>
 

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