Retain NSTimer instance


#1

Hi everybody
I wonder why Aaron retain his NSTimer example on page 315 (3rd edition).
NSTimer was not alloc, copy or something.
Why does it need to be retained?

Thanks for the answer
Markus


#2

Sorry for the question
Found the answer myself

markus


#3

[quote=“markusbuerer”]Sorry for the question
Found the answer myself

markus[/quote]

and the answer is? :slight_smile:


#4

I like to copy 2 sentence from the book (page 73)

  • Objects created by alloc, new, copy or mutableCopy have a retain count of 1 and are not in the autorelease pool.
  • If you get an object by any other method, assume that it has a retain count of 1 and is in the autorelease pool. If you do not wish it to be deallocated with the current autorelease pool, you must retain it

hope that’s the correct answer
markus


#5

[quote=“markusbuerer”]I like to copy 2 sentence from the book (page 73)

  • Objects created by alloc, new, copy or mutableCopy have a retain count of 1 and are not in the autorelease pool.
  • If you get an object by any other method, assume that it has a retain count of 1 and is in the autorelease pool. If you do not wish it to be deallocated with the current autorelease pool, you must retain it

hope that’s the correct answer
markus[/quote]

Yeah I think that’s why you have to retain it.

Another interesting question would be what happens when you turn on garbage collection, can you omit the retain in this case? If yes, why? :slight_smile: