simple question about variable retain/release (NSDate)
simple question about variable retain/release (NSDate)
- Subject: simple question about variable retain/release (NSDate)
- From: Bob Sabiston <email@hidden>
- Date: Fri, 21 Nov 2008 20:17:17 -0600
Hi,
I have a newbie question about retain/release stuff when using
NSDate. I am confused a little about what is necessary in this case.
I've got a local variable sTime (NSDate *). I use it to measure the
time and then compare it to the current time, so that I can space out an
animation over one second.
The docs say that [NSDate date] creates and returns an NSDate. Does
that mean the memory's been allocated until I release it?
Here's what I have. It works until the very end, where it crashes.
NSDate *sTime = [NSDate date]; // get beginning time
NSTimeInterval elapsedTime;
float frameduration = 1.0/30.0; // for 30 fps
for (int f = 0; f < 30; f++) {
ratio = ((float)f/framerate);
// animation interpolation here based on ratio
// now wait for 1/30th of a second
do {
elapsedTime = -[sTime timeIntervalSinceNow];
}
while (elapsedTime < frameduration);
[sTime release];
sTime = [NSDate date];
[self drawView];
}
[sTime release];
sTime = NULL;
So, you see that whenever one frame's worth of time has passed, I
release the NSDate and set sTime again to [NSDate date]. Do I need to
do that?
Why does it crash at the end? sTime is just a pointer. And if that
final release is crashing it, why do all of the previous ones in the
for-loop not crash it?
I could do the same thing just by measuring the time once and then
just use the elapsed time incrementally for measurement, but I would
like to understand what it is that is causing this crash the way it is
now. Is the [NSDate date] call something I don't need to worry about
with regard to memory? That is, can I set it and re-set it over and
over, then finally leave the function without worrying about what
happened to all of those calls?
Thanks for any info.
Bob
_______________________________________________
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