Re: NSDate scope
Re: NSDate scope
- Subject: Re: NSDate scope
- From: Ken Thomases <email@hidden>
- Date: Sun, 21 Jun 2009 15:46:29 -0500
On Jun 20, 2009, at 6:52 PM, John Baldwin wrote:
I declared a (NSDate *) in my .h file.
NSDate *originalDate;
The above is unclear. You mean you declared an instance variable in
some class with type NSDate*?
Then in my init method, I initialized it to the current date. I
tried various methods, all with the same result:
originalDate = [NSDate date];
originalDate = [NSDate dateWithTimeIntervalSinceNow:0];
originalDate = [[NSDate alloc] init];
originalDate = [[NSDate date] retain];
(I probably tried others, too.)
Flailing about like this means you're not taking the time to
understand what you're doing (or trying to do). You need to (re-)read
the Memory Management Guide and perhaps some of the other basic
conceptual documentation.
The first two of your examples have the same serious bug. You're not
arranging to be sure the object will live as long as you need it to.
I'd be surprised if all of those approaches really did produce the
same result. Are you sure you implemented the initializer that's
actually being used? Maybe your initializer is never called, leaving
the originalDate pointer as nil -- all fields of an object are zeroed
out when the object is allocated.
Try putting in an NSLog statement to show the date object that you've
obtained.
Then, in a method that was called by an NSTimer object, I tried to
use that date as a reference point. But the debugger showed the
variable as "out of scope."
This message from the debugger may be benign or insignificant. It
doesn't necessarily mean there's any real problem. Are you debugging
a build that's been optimized or lacks debugging symbols?
NSDate *currentDate = [NSDate date];
NSTimeInterval secs = [currentDate timeIntervalSinceDate
originalDate];
The variable would be out of scope, and the program would crash upon
referencing it.
If the variable were really out of scope, the compiler would have
refused to compile your program and you wouldn't get as far as trying
to run it (or encountering a crash).
The crash is most likely due to a problem with your memory management,
as mentioned above. The object pointed to by originalDate was
probably allowed to be deallocated (because you didn't exert your own
control over its lifetime), leaving the pointer pointing to garbage.
First, make sure you're following the memory management guidelines.
If the problem persists, you can enable NSZombie to help track down
the problem, possibly along with the ObjectAlloc instrument.
In any case, if you ask the list to help you diagnose a crash, you
need to supply the details of the crash. What's the actual signal or
exception encountered (e.g. EXC_BAD_ACCESS)? What's the backtrace?
Also, it can't hurt to put in another NSLog statement here showing the
current value of originalDate.
Regards,
Ken
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
References: | |
| >NSDate scope (From: John Baldwin <email@hidden>) |