1

My question is why

[myButton addTarget:self action:@selector(myAction) forControlEvents:UIControlEventTouchUpInside];

it is not retain and

mytimer = [NSTimer scheduledTimerWithTimeInterval:1.0f
                                          target:self
                                        selector:@selector(_timerFired:)
                                        userInfo:nil
                                         repeats:YES];

is retaind ,where both have the target self so why one is retain and other is not retaind. @end

Sishu
  • 1,510
  • 1
  • 21
  • 48

1 Answers1

2

The NSTimer created with +[NSTimer scheduledTimerWithTimeInterval:…] is scheduled on the current run loop, and the run loop itself retains the timer.

You can see this if you profile your program with the allocations tool and search for living instances of CFRunLoopTimer. You'll see that under your call to scheduledTimerWithTimeInterval:… there is a call to CFRunLoopAddTimer, and the timer is retained under that call.

So the fact that the timer is retained and the button is not retained has to do with the scheduling of the timer on the run loop and has nothing to do with the target of either object.

You may find the following reference materials helpful:

If you're starting a new project you should certainly be using Automatic Reference Counting (and Xcode will have that turned on for you by default, when you create a new project). That system doesn't entirely free you from the responsibility of thinking about your program's memory usage, but it will make things easier.

I included a reference to run loop management, but you probably will not need to do any manual run loop management. It's just good to have a high level understanding of what "the run loop" means, for when it comes up in other documentation.

Aaron Golden
  • 7,092
  • 1
  • 25
  • 31
  • thanx for your reply,but please explain little bit coz i m new to iphone developement and can't understand that much deep.Little bit explanation please ,if you can. – Sishu Jul 17 '14 at 06:28
  • A reasonable explanation of the reference counting system and run loops would be too much for this answer, but I'll add some links to references you may find helpful. – Aaron Golden Jul 17 '14 at 06:33