运行popToRootViewControllerAnimated 返回最上层后,dealloc运行报错。

本文探讨了iOS应用中UITextField的内存管理问题,特别是在pushViewController和popToViewController操作中出现的EXC_BAD_ACCESS错误。通过代码示例分析了autorelease与手动release的问题,并讨论了如何避免这些错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码如下:
@interface A : UIViewController <UITextFieldDelegate>
{
    UITextField *inputName;
}

@property (nonatomic,retain) UITextField *inputName;

@end


....m文件
@implementation A

@synthesize inputName;

- (void)dealloc
{
    if(inputName != nil)
    {
        [inputName release];
        inputName = nil;
    }
   [super dealloc];
}
//A类的dealloc函数调用后报错。EXC_BAD_ACCESS(code = 1,address=0xd000000c)

pushViewController代码如下:
aa = [[A alloc] init];
        [self.navigationController pushViewController:aa animated:YES];
        [aa release];
        aa = nil;

popToViewController 代码如下:
NSArray *array = self.navigationController.viewControllers;
    B *app = (B *)([array objectAtIndex:[array count] - 3]);
    [self.navigationController popToViewController:(UIViewController *)app animated:YES];
    //[self.navigationController popToRootViewControllerAnimated:YES];

在执行popToViewController 或popToRootViewControllerAnimated,返回最顶层后,A类的dealloc函数调用到了,然后报错。EXC_BAD_ACCESS(code = 1,address=0xd000000c)错误
在init里面 inputName = [[UITextField alloc]initWithFrame:CGRectMake(10, Img.size.height + Img.size.height / 2, Img.size.width,Img.size.height)];
    inputName.delegate = self;
    [self.view addSubview:inputName];
    [inputName release];
在这个地方inputName release有什么问题吗?如果我把[inputName release];这条语句去掉,dealloc里面就不报错了,我跑了retainCount语句,addSubview之后retainCount是2,在 dealloc里面retainCount输出也是2,我的整个做法哪里有问题吗?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值