topgun
topgun

Reputation: 2583

Some memory management understanding

I have created a property of a viewController and retaining it from ClassB of viewController (Class A).

so basically I have @property (nonatomic, retain) ClassAViewControllerVC, and synthesized in the main file.

I have an IBAction in which I am allocating ClassAViewController and pushing it on navigation stack, but I am trying to analyze where should I release this viewController?

- (IBAction) response {

 ClassAViewControllerVC = [ClassAViewController alloc] initWithNib:@"ClassAViewController" bundle:[NSBundle mainBundle]];

 [self.navigationController pushViewController:self.ClassAViewControllerVC animated:YES]

}

Is it okay to release the view controller after I stack it on the navigation-Controller as described above?

Also, is it a good idea to set property for such viewController at the first place? I started to notice that my apps started to crash if not utilized @property retain way. Any thoughts or concern would be appreciated.

Thanks

Upvotes: 1

Views: 96

Answers (3)

AAV
AAV

Reputation: 3803

Best way to solve this problem is, drag and drop empty UIViewController in your ClassB.xib file. Create @property (nonatomic, retain) ClassAViewControllerVC, make the connection to empty UIViewController you just drop to classB.xib file and also change the NIB name to the class A nib file name. This will solve all the problem of crashing because you are not allocating or releasing any memory.

Upvotes: 0

dariaa
dariaa

Reputation: 6385

if ClassAViewControllerVC is a field of ClassB, you should release it in the dealloc method.

But you should probably create ClassAViewControllerVC in the init or even better viewDidLoad method.(note: if you create something in viewDidLoad, you need to release it viewDidUnload as well, not only in dealloc)

Because now every time "response" method is called ClassAViewControllerVC will be created all over again. (and there is not much sense storing it in the property). If this is what you want you probably should not make ClassAViewControllerVC a filed of your ClassB, but just have it as s local variable in response method like this:

- (IBAction) response {
    ClassAViewController *classAViewController = [ClassAViewController alloc] initWithNib:@"ClassAViewController" bundle:[NSBundle mainBundle]];

    [self.navigationController pushViewController:classAViewController animated:YES];
    [classAViewController release];

}

Upvotes: 0

joerick
joerick

Reputation: 16448

Firstly, no, it's not necessary to keep such an object in a property. You only need to keep objects in a property if the class will require access to the object later on. In this case, I think a local variable will do.

In this example, you create a ClassAViewController with alloc, meaning that the caller (this method) has responsibility to release it once it's finished with it.

When you add it to the navigation controller stack, the navigation controller retains it, because it keeps a reference to it.

So, at the end of this method, you should release it, but it's been retained by the navigation controller, so it's not deleted.

The code should look like this:

- (IBAction) response {
    ClassAViewController *viewController = [ClassAViewController alloc] initWithNib:@"ClassAViewController" 
                                                                             bundle:[NSBundle mainBundle]];

    [self.navigationController pushViewController:viewController animated:YES]

    [viewController release];
}

P.S. it's convention in objective-C to write variable names starting with a lowercase letter. Uppercase starting letters are used for class names, and it confuses the bejeesus out of me! ;)

Upvotes: 3

Related Questions