Ben Thompson
Ben Thompson

Reputation: 4833

Custom UITableViewCell crashing on [super dealloc]

I have a subclass of a UITableViewCell which is crashing when it hits the [super dealloc] line. I have several textFields in my cell and the error message is saying *** -[UITextField release]: message sent to deallocated instance 0x739dfd0

The relevant code snippets are below (I do have other textFields but they are all treated the same way. My suspicion is it is to do with adding it to the contentView of the cell. But I do not know how to correct it.

.h file of Custom UITableViewCell:

@interface ExerciseTableViewCell : UITableViewCell {

    UITextField *textField1;

}

@property (nonatomic, retain) UITextField *textField1;

@end

.m file:

@implementation ExerciseTableViewCell

@synthesize textField1;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code

    UIView *myContentView = self.contentView;

        UITextField *newTextField1 = [[UITextField alloc] init];

        self.textField1 = newTextField1;

        [newTextField1 release];

        [myContentView addSubview:textField1];

    }
    return self;
}


}

- (void)dealloc {
    [textField1 release];
    [super dealloc];
}

I cannot see why I would be releasing the textField one too many times?

Upvotes: 1

Views: 762

Answers (3)

Charan
Charan

Reputation: 4946

what is the need of declaring textfield locally and assigning it to the globally declared text field, just use

textField1 = [[UITextField alloc] init]; 
[myContentView addSubview:textField1];

Upvotes: 1

Tarun
Tarun

Reputation: 766

Set textfield object to nil instead of releasing it. Second, please use a proper naming convention while coding.

Upvotes: 0

graver
graver

Reputation: 15213

Change:

UITextField *newTextField1 = [[UITextField alloc] init];

self.textField1 = newTextField1;

[newTextField1 release];

[myContentView addSubview:textField1];

to:

self.textField1 = [[[UITextField alloc] init] autorelease];
[myContextView addSubview:self.textField1];

Upvotes: 2

Related Questions