开发者

Memory leak at text field in iphone sdk

I am getting memory leak at text fiels please suggest me how to resolve it.

I written the code in init() as:

            eventTextField = nil;
    eventTextField = [[UITextField alloc]initWithFrame:CGRectMake(10, 15, 300, 50)];
    eventTextField.placeholder = @"Event Name";
    [eventTextField setFont:[UIFont boldSystemFontOfSize:14]];
    eventTextField.returnKeyType = UIReturnKeyDone;
    eventTextField.keyboardAppearance  = UIKeyboardAppearanceDefault;//Showing leak at this line
    eventTextField.keyboardType = UIKeyboardTypeDefault;
    eventTextField.delegate=self;

and I released it in dealloc method.

and in cellForRowIndex I am adding it as subview.

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MasterViewIdentifier"];
if (cell == nil) 
{
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"MasterViewIdentifier"] autorelease];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    UIView* elementView =  [[UIView alloc] initWithFrame:CGRectMake(20,170,320,280)];
    elementView.tag = 0;
    elementView.backgroundColor=[UIColor clearColor];
    [cell.contentView addSubview:elementView];
    [elementView release];
}
UIView* elementView  = [cell.contentView viewWithTag:0];
elementView.backgroundColor=[UIColor clearColor];
for(UIView* subView in elementView.subviews)
{
    [subView removeFromSuperview];
}
if(indexPath.section == 0 && indexPath.row == 0)
{
    CorkItAppDelegate* appDelegate =开发者_运维技巧 (CorkItAppDelegate*)[[UIApplication sharedApplication] delegate];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    wineNameTitleLabel.numberOfLines = 0;
    [wineNameTitleLabel setFont:[UIFont boldSystemFontOfSize:14]];

    if(appDelegate.isNewWine == YES)
    {
        wineNameTitleLabel.textColor = [UIColor blackColor] ;
        wineNameTitleLabel.text = appDelegate.getNewWineName;
    }
    else
    {
        if([event.eventWineName length]>0)
        {
            wineNameTitleLabel.textColor = [UIColor blackColor] ;
            wineNameTitleLabel.text = event.eventWineName;
        }
        else
        {
            wineNameTitleLabel.text = @"Wine Name";
        }
    }
        [elementView addSubview:wineNameTitleLabel];
}

else if(indexPath.section == 1)
{

    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;
}
       return cell;
 }

Hope I get wucik response from your side. Thanks in advance, Monish.


If you still need the eventTextField. You should create a property and instance variable for it. So that, you can your class own the eventTextField and release it in the dealloc. Like this:

@interface A {
  UITextField *eventTextField;
}

@property (nonatomic, retain) UITextField *eventTextField;

@end

@implementation A
@synthesize eventTextField;

- (id)init {
  eventTextField = [[UITextField alloc]init];
}

- (void)dealloc {
  [eventTextField release];
}
@end


you should release the UITextField after adding it to the subview

[foo addSubview:eventTextField];
[eventTextField release];

eventTextField = nil; is unnecessary btw as eventTextField is a ivar is already zeroed (pointer set to 0x0(nil))

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜