Gil
Gil

Reputation: 103

Custom subview gives memory leak when analyzed

I am trying to create my own custom subview for an app. The view is a subclass of UIView and contains a couple of UIButtons and a UITextField. The code actually builds and runs without problems, but when I do an "Analyze" of the code it gives me 4 potential memory leaks. I will be including this subview in a larger project so I want to nip any errors here before adding it to the bigger project. Here's the code where one of the leaks occurs:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.nameView.delegate = self;
    [self.nameView.newName resignFirstResponder];   // this is line 60
    [self.nameView setHidden:YES];
    [self.nameView setNeedsDisplay];
}

The error it gives me is:"Potential leak of an object created on line 60" Then if I click on that error is says: "Object allocated on line 60 is not referenced later in this execution path and has a retain count of +1, object leaked" Thanks in advance for any help you can give me. Gil

CocoaFu, thanks for the help - I am new to the site and I will try to give the proper feedback. As far as this problem goes I should have included a bit more information. nameView is the name of my custom view newName is the name of a UITextField in the view. I will try then suggestions here and let you know if they work. Thanks again. This is a great site!

Upvotes: 1

Views: 92

Answers (1)

zaph
zaph

Reputation: 112857

[self.nameView.newName resignFirstResponder];

is the same as:

[[[self nameView] newName] resignFirstResponder];

which means that newName is a method and methods that begin with new or copy are expected to return a retained instance. I suspect that newName does not do that but the rules say it does. The solution is to follow Objective-C naming rules: change the name newName.

Not, you don't say but perhaps newName is a property. Well, @synthesize creates accessor methods newName and setNetName.

Upvotes: 1

Related Questions