简体   繁体   English

调用发布方法导致iOS应用崩溃

[英]Calling release method crashes iOS app

I have subclassed UIView as a Toolbar and add all kinds of buttons and other views to the class. 我将UIView子类化为工具栏,并向该类添加了各种按钮和其他视图。 Now, in my ViewController header I have this: 现在,在我的ViewController标头中,我有这个:

@interface GridViewController : UIViewController <UIWebViewDelegate, UIScrollViewDelegate> {
    Toolbar *toolbar;
}

@property (retain) Toolbar *toolbar;

and in the implementation I have this: 在实现中,我有:

@synthesize toolbar;

- (void)viewDidLoad {
    [super viewDidLoad];
    CGRect toolbarRect = CGRectMake(0, 0, 1024, 40);
    self.toolbar = [[Toolbar alloc] initWithFrame:toolbarRect];
    [self.view addSubview:toolbar];
}

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

Now, if I run analyze, it basically says that I have a potential memory leak. 现在,如果我运行分析,它基本上说我有潜在的内存泄漏。 If I add [toolbar release]; 如果我添加[工具栏发布]; right after calling addSubView:toolbar like I do usually when adding subviews the app crashes when I pop the ViewController. 在像我通常在添加子视图时通常那样调用addSubView:toolbar之后,当我弹出ViewController时应用崩溃。

What am I doing wrong? 我究竟做错了什么? I've tried reading all about the memory management and according to that you always have to release/autorelease the things you alloc/copy/retain etc. If I look at my code, I'm retaining the instance variable in the header and also allocating it in the implementation, so the retain count should be +2 and thus I'd need to release it twice, but it seems I have misunderstood something. 我已经尝试阅读所有有关内存管理的内容,并根据这些内容,您始终必须释放/自动释放您分配/复制/保留的内容。如果查看代码,我会将实例变量保留在标头中,并且还将在实现中分配它,因此保留计数应为+2,因此我需要将其释放两次,但是似乎我误解了一些东西。 Any help is very much appreciated. 很感谢任何形式的帮助。

When I say crash, I get this: Xcode pauses and says that EXC_BAD_ACCESS for this line in main.m 当我说崩溃时,我得到了:Xcode暂停并说main.m中此行的EXC_BAD_ACCESS

int retVal = UIApplicationMain(argc, argv, nil, nil);

and in console I get this: 在控制台中,我得到以下信息:

modifying layer that is being finalized - 0x60895f0 

If the property self.toolbar is set to (retain) it suggests that after the line self.toolbar = [[Toolbar alloc] initWithFrame: toolbarRect]; 如果将self.toolbar属性设置为(retain)则建议在self.toolbar = [[Toolbar alloc] initWithFrame: toolbarRect]; the retain count should be +2. 保留数应为+2。 I'd suggest replacing that line with self.toolbar = [[[Toolbar alloc] initWithFrame: toolbarRect] autorelease] to make the retain count remain at 1. That should probably fix it, as the retain that comes from addSubview is not managed by you and you release toolbar in the dealloc . 我建议用self.toolbar = [[[Toolbar alloc] initWithFrame: toolbarRect] autorelease]替换该行,以使保留计数保持为1。这可能应该解决,因为addSubview的保留不受您和您释放了dealloc toolbar

All properties with retain setters should be given an autoreleased object. 具有保留设置器的所有属性都应被赋予一个自动释放的对象。

It seems that the problem was actually in the Toolbar class and not the ViewController. 看来问题出在实际上是在Toolbar类中,而不是在ViewController中。 I wasn't using setters for the ivars and thus it started breaking. 我没有使用setter来作为ivars,因此它开始崩溃了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM