Gestión de memoria del iPhone: una versión después de configurar self.someProperty = nil

Estoy leyendo el LazyTableImages code that Apple have released and they do something to this effect (in an NSOperation subclass):

- (void)dealloc {
    [myProperty release];
    [myProperty2 release];

- (void)main {
    // Parse operation undertaken here
    self.myProperty = nil;
    self.myProperty2 = nil;

My thinking is that they do this en caso dealloc is called before setting properties to nil.

¿Mi pensamiento es correcto aquí? Are the releases unnecessary, ya que self.myProperty = nil effectively releases myProperty?

One thing I have noticed in this code is that they don't release all retained objects in dealloc, only some of them, which is really the cause for my confusion.


preguntado el 08 de enero de 11 a las 22:01

Don't forget to send [super dealloc]. You will leak the object if you don't. -

1 Respuestas

No puedes hacer self.myProperty = nil o incluso [myProperty release] Si lo envía después dealloc. Know why? Porque self doesn't exist any more.

As for your question, it appears that the releases in dealloc are unnecessary, but it's still good practice to clean up your instance variables in your dealloc método.


As Peter points out in the comment, if the -main method is never executed, then having the release statements in -dealloc is necessary; without them, you'd be leaking memory.

Respondido el 09 de enero de 11 a las 03:01

The releases in dealloc Médica necessary if main is never called (if, for example, this operation is never enqueued) or if either or both of the properties are changed to point to objects again after main returns (i.e., after the operation finishes). I'd go to the opposite end: It's nil-ing out the properties in main that's unnecessary. - Peter Hosey

@Peter good point (edited answer). I was answering under the assumption that main would always get invoked. - Dave DeLong

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.