views:

57

answers:

3

If I release the dueDate here I am having BAD_EXCESS in other place of my code. What am I doing wrong here? invoice is a core date object/entity here.

NSDate *deliveryDate = [NSDate dateWithTimeIntervalSinceNow: - oneDayInSeconds * 7];
NSDate *dueDate = [[NSDate date] initWithTimeInterval:(NSTimeInterval) (oneDayInSeconds * 3) sinceDate:deliveryDate]; 
[invoice setDueDate:dueDate];  
//[dueDate release];
+7  A: 

Use

[NSDate alloc]

instead of

[NSDate date]
nschum
it did not work. It seems the 'invoice' object keep pointer to that dueDate. When I debug in other classes to get the dueDate, it show 'out of scope'.
karim
If you want 'invoice' to keep 'dueDate', 'setDueDate' has to retain it. Otherwise it is deallocated by 'release' and nobody may use it anymore.If you don't want 'invoice' to keep 'dueDate', you should set it to nil yourself. Deallocated objects don't magically become nil.
nschum
as said, 'invoice' is a core data object. seems core data fields setters do not retain the object. How can I force it to retain its parameter?
karim
I overlooked that. CoreData objects should generally retain the objects. If that doesn't work, you might have problems beyond the scope of this question and that are difficult to analyze in a comment. Please post more details and code or a separate question. Your original question is answered.
nschum
+2  A: 

Change [NSDate date] to [NSDate alloc].

David
is there really any difference. [NSDate date] also returns a new date object.
karim
There is a difference. [NSDate date] returns an initialized and autoreleased object. [NSDate alloc] returns an uninitialized object with a reference count of 1.
nschum
A: 

As said, change [NSDate date] to [NSDate alloc].

If you are "init"ing you need to "alloc" too.

Alternatively you could do this:

NSDate *dueDate = [NSDate dateWithTimeInterval:sinceDate:]

or even:

NSDate *dueDate = [NSDate date]; dueDate = [NSDate dateWithTimeInterval:sinceDate:]

etc etc... :)

Thomas Clayson
dateWithTimerInterval is not available in iPhone OS 3.x
karim
I merely copied the code from the question to explain the differences. :)
Thomas Clayson