views:

52

answers:

2

Hi guys,

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.

A: 

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))

valexa
But I am getting Exception.I am using the textfield in keyBoardWillShow method and textfieldDidchange and in DoneAction.
"Exception" ? If you do as i suggested remove the other releases you do on the object. addSubview: retains the object so if you add it as a subview after you create the object you are safe to release it and it still exists until it is removed from the subview, so you can using it wherever you wish
valexa
Hum, for his posted code. he addSubView but then he removeFromSuperView in some time so at that time, eventTextField becomes gabarged
vodkhang
A: 

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
vodkhang
I had done with ur suggestion even though it showing memory leak.Is there any other suggestions from ur side?
sorry, I didn't read your question clearly. You released it in dealloc, so this code will not help. Do you add your eventTextField to any subview? or remove it anywhere?
vodkhang
I posted the code where I made subview.Please suggest me with any other ideas.
Hum, that explains for me everything about crash. You will remove that subview but only in some case, you add it back. So, I think that in the dealloc you only need to release the tableView and the eventTextField as my old suggestion. You own the eventTextField and finally you released it, that should not have any problems. I think either xcode goes wrong or leak in somewhere else
vodkhang