priya
priya

Reputation: 57

iphone-textfield releasing causes crash application

sir i am new in iphone i am creating UITextField using this code but when i am releasing this in dealloc application crashes. i want to make textfield by coding. thanks in advance.

#import "TextField.h"

@implementation TextField
UILabel *label;
UITextField *textField;

- (void)viewDidLoad 
{
    [super viewDidLoad];

    //Create label
    label = [[UILabel alloc] init];
    label.frame = CGRectMake(10, 10, 300, 40);
    label.textAlignment = UITextAlignmentCenter;
    label.text = @"";
    [self.view addSubview:label];
    [label release];

    // Initialization code
    textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, 300, 50)];
    textField.delegate = self;
    textField.placeholder = @"<Enter Text>";
    textField.textAlignment = UITextAlignmentCenter;
    [self.view addSubview: textField];
    [textField release];
}

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

@end

Upvotes: 0

Views: 200

Answers (3)

Arslan
Arslan

Reputation: 919

you are using [textField release] in two places:
1) in ViewDidLoad
2) in dealloc method
You don't have to do this in two placed. Remove the [textField release] from viewDidLoad method.

This is because when you alloc an instance it retain count becomes 1 and when you are release it, the retain count becomes 0. So again release the same instance is causing the crash.

Upvotes: 0

Maulik
Maulik

Reputation: 19418

You have already released your text field and label by :[textField release]; ,[label release]; than you should not release it again in dealloc method.You are over releasing your text filed and label and that cause crash your application. Just remove it from dealloc method.

Upvotes: 1

Deepesh
Deepesh

Reputation: 643

use this code... u have release label and text field two times ...

#import "TextField.h"

@implementation TextField
UILabel *label;
UITextField *textField;

- (void)viewDidLoad {
[super viewDidLoad];

//Create label
label = [[UILabel alloc] init];
label.frame = CGRectMake(10, 10, 300, 40);
label.textAlignment = UITextAlignmentCenter;
label.text = @"";
[self.view addSubview:label];
[label release];

// Initialization code
textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, 300, 50)];
textField.delegate = self;
textField.placeholder = @"<Enter Text>";
textField.textAlignment = UITextAlignmentCenter;
[self.view addSubview: textField];
[textField release];
}

@end

Upvotes: 0

Related Questions