Re: NSTimer
Re: NSTimer
- Subject: Re: NSTimer
- From: Sherm Pendley <email@hidden>
- Date: Tue, 3 Apr 2007 16:03:20 -0400
On Apr 3, 2007, at 3:35 PM, Bill Allaire wrote:
I'm new to Cocoa, so please bear with me.
I initialize a timer when overriding init:
timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self
selector:@selector(setDevState:) userInfo:nil repeats:YES];
I create an array to manipulate during each timer event in
awakeFromNib:
tableArray = [NSArray arrayWithObjects:aisle1Table, aisle2Table,
aisle3Table, nil];
The +arrayWithObjects: method isn't +alloc or -copy, so tableArray
hasn't been implicitly retained for you. So, if you want to be able
to use tableArray later you need to explicitly -retain it. (And don't
forget to release it when you're done with it.)
In awakeFromNib: I call:
[self setDevState:timer];
When the program runs, the three tables are colored according to
their status (online or offline) as expected. In the debugger the
tableArray has a count of three objects. When the timer fires and
enters setDevState: the count of tableArray shows and when this
object is accessed, GDB is brought up:
{(int)[$VAR count]} objects
If I comment out the line [self setDevState:timer]; and wait for
the timer to fire, the debugger is brought up with the same
behavior: no count in the array. I don't understand why I can call
setDevState: outside a timer and it works fine but when called by
way of the timer things go awry.
TableArray is autoreleased, but when you call it in awakeFromNib:
you're still in the same pass through the event loop in which
tableArray was created, so the autorelease pool it's in hasn't been
released yet. You're getting the timer callback in another iteration
of the event loop, where the autorelease pool has already been
released, taking tableArray with it.
Have a look at:
<file:///Developer/ADC Reference Library/documentation/Cocoa/
Conceptual/MemoryMgmt/index.html>
Or online:
<http://developer.apple.com/documentation/Cocoa/Conceptual/
MemoryMgmt/index.html>
sherm--
Web Hosting by West Virginians, for West Virginians: http://wv-www.net
Cocoa programming in Perl: http://camelbones.sourceforge.net
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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: | |
| >NSTimer (From: Bill Allaire <email@hidden>) |