开发者

Getting Memory Leak in textFields

Here Im Getting memory leak at

eventTextField.keyboardAppearance  = UIKeyboardAppearanceDefault;

I declared the textfield globally and I allocated the text field in CellForRowAtIndex and my code is:

if(indexPath.section == 1)
{
    eventTextField = [[UITextField alloc]initWithFrame:CGRectMake(10, 15, 300, 50)];
    eventTextField.placeholder = @"Event Name:";
    [eventTextField setFont:[UIFont boldSystemFontOfSize:14]];
    eventTextField.returnKeyType = UIReturnKeyNext;
    eventTextField.keyboardAppearance  = UIKe开发者_开发知识库yboardAppearanceDefault;
    eventTextField.keyboardType = UIKeyboardTypeDefault;
    eventTextField.delegate=self;
    if(isRightButton == YES)
    {
        eventTextField.enabled = NO;
    }
    else
    {
        eventTextField.enabled = YES;
    }
    if([event.eventName length] > 0)
    {
        eventTextField.text = event.eventName;
    }
    else
    {
        eventTextField.text = @"";
    }
    [elementView addSubview:eventTextField];
    cell.accessoryType = UITableViewCellAccessoryNone;
}

and I release the textfield in dealloc. when Im checking the object allocations it showing leak at: eventTextField.keyboardAppearance = UIKeyboardAppearanceDefault;

guys please help me to get out of this. Anyone's help will be appreciated.

Thank you, Monish Kumar.


Simply add:

[eventTextField release]

after:

[elementView addSubview:eventTextField];

This works because addSubview will retain the subview and release it when the view itself is released, so you can safely release the textfield after adding it to the view.

Golden Rule: Anytime you call alloc or copy or retain, you should always call release (or autorelease).


I would say it's because every time cellForRowAtIndexPath get's called, your allocating memory to eventTextField, without first releasing what you had before.

Check if eventTextField is nil before doing the alloc.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜