views:

472

answers:

2

I have a class which is responsible and the owner of building my model objects. It allocates memory for the models and has them retained and is responsible for releasing them when dealloc happens.

Now I have a respective UIViewController which is acting as the client in this case. It will have several instance variables, pointing to the model(s) it needs. It doesn't need to allocate the memory for them as the class responsible for doing so has already done it. Do I still need to release the memory from the client? Here's an example

ModelHolder.m will have something like

- (NSArray *)modelA
{
   if (modelA == nil) {
       modelA = [[ModelClassA alloc] init];
   }

   return modelA

}

- (void)dealloc { [super dealloc]; [modelA release]; }

Now the ClientViewController will have something similar:

 @class myModelA;
 @interface ClientViewController : UIViewController {
     ModelClassA myModelA;
 }

 // more code
 @end
 #import "ModelHolder.h"
 @implementation ClientViewcontroller ...... etc
 - (void)viewDidLoad
 {  
     self.myModelA = [instanceOfModelHolder modelA];
 }
 - (void)dealloc {
     // am I responsible to release here?
 }

Please don't mind any syntactical errors as I just wrote this on the fly here. I think my point was illustrated fine in code though.

Thank you..

A: 

You should put [super dealloc]; last in your own dealloc.

- (void)dealloc
{
    [modelA release];
    [super dealloc];
}

As for your question about releasing in the last dealloc it depends on how you specified the @property myModelA, is it a "retain" or a "copy" property you should do a release. Is it a "copy" you are actually in charge of a new object.

epatel
Right, as I mentioned, don't mind anything missing. The original question still stands in as far as memory management goes.
Coocoo4Cocoa
Was typing it just now... :)
epatel
+3  A: 

Assuming that you declared ClientviewController's modelA property as either @property(retain), @property(copy), or @property(mutableCopy), you are retaining the value of that property, so you must release it.

Peter Hosey