Re: simple question about variable retain/release (NSDate)
Re: simple question about variable retain/release (NSDate)
- Subject: Re: simple question about variable retain/release (NSDate)
- From: Andrew Farmer <email@hidden>
- Date: Fri, 21 Nov 2008 18:30:05 -0800
On 21 Nov 08, at 18:17, Bob Sabiston wrote:
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?
See the Cocoa memory management rules:
http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/Tasks/MemoryManagementRules.html
As the selector name "date" doesn't start with "alloc" or "new", and
doesn't contain "copy", you don't own this object, making all the
releases on sTime incorrect. The crashes are occurring when the
enclosing NSAutoreleasePool gets purged (but that's an implementation
detail - the part that matters is that you're releasing objects that
you never owned).
Here's what I have. It works until the very end, where it crashes.
...
// now wait for 1/30th of a second
do {
elapsedTime = -[sTime timeIntervalSinceNow];
}
while (elapsedTime < frameduration);
This is incredibly inefficient. Use
[NSThread sleepUntilDate:[NSDate
dateWithTimeIntervalSinceNow:frameduration]]
instead. (But realize that this'll run slightly under 30 FPS, as the
drawing takes some time.)
_______________________________________________
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