Re: NSTimer problem
Re: NSTimer problem
- Subject: Re: NSTimer problem
- From: Keary Suska <email@hidden>
- Date: Thu, 17 Jan 2008 12:39:58 -0700
- Thread-topic: NSTimer problem
on 1/17/08 11:21 AM, email@hidden purportedly said:
> timer is an instance variable, not a local variable. Sorry if that wasn't
> clear.
It was clear in the code, just that the coding conventions you are using
are, well, unorthodox. These could come back to bite you.
> I have read the guidelines. This one is technique 2 on this page:
> http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/
> mmAccessorMethods.html#//apple_ref/doc/uid/TP40003539
> But I have used all three listed on the page, with 3 being the most
> typical for my uses. If I understand the process correctly, the
> autorelease tells the runtime to decrement the retain at some point in
> future, most likely after the function returns. The retain increments the
> retain count immediately, so "at point in the future" there should an
> offset in retain/releases and the retain count for my object should be
> correct. Am I misunderstanding something? This is important to me since I
> have quite a bit of code written like this and I just don't find leaks
> using tools such as instruments now or in the past with leaks or
> ObjectAlloc.
I think you mean technique 2. Anyway, the problem is not with your accessor,
but with your reset method, where you alloc the timer instance variable, an
then pass it to the setter. If you use that pattern frequently, you probably
have a big mess.
In you specific code, when you alloc the timer, the object has a retain
count of 1. Naturally. Then you call setTimer:, and that method autoreleases
the timer instance. Cool--that offsets the alloc. But then you retain the
timer object, which at that point increases the retain count to 2.
Whoops--now when the autorelease pool is cleared, your object as a retain
count of 1 and will not be released. Hence, it leaks. This happens because
you are alloc/initing the instance variable, and then passing it to
setTimer. Thus, in setTimer, the argument "tm" and the variable "timer" are
exactly the same object.
Also, AFAIK, you don't use ObjectAlloc for leaks, you use MallocDebug. I
don't know about Instruments, but if it is designed to detect leaks, and it
doesn't detect any, there must be a convolution in your code that the tool
can't deal with. I can tell you for certain that after the first call to
-resetTimer:, every subsequent call is leaking exactly one NSTimer object.
>> It always will, the first time. Once it has been created, it should no
>> longer be null.
>
> So how can I get a valid reference to the timer?
I don' understanding what you are asking. When your object is first created,
all instance variables that are objects are set to nil. At some point, the
timer property is set with a valid object. Until that point, there will be
no valid object. Why would you expect otherwise?
Best,
Keary Suska
Esoteritech, Inc.
"Demystifying technology for your home or business"
_______________________________________________
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