简体   繁体   English

我可以解决的崩溃,但我不明白为什么会发生这种情况

[英]A crash I can fix, but I don't understand why it's happening

I have a scrollview. 我有一个scrollview。 I add a button to this scrollview and release it after. 我在此滚动视图中添加了一个按钮,然后将其释放。

UIButton * saveButton = [UIButton buttonWithType:UIButtonTypeCustom];
saveButton.frame = CGRectMake(415.0, 473, 80, 38);
saveButton.titleLabel.font = [UIFont fontWithName:@"Heiti TC" size:24];
[saveButton setTitle:@"" forState:UIControlStateNormal];
[saveButton setContentEdgeInsets:UIEdgeInsetsMake(2, 0, 0, 0)];
saveButton.backgroundColor = [UIColor clearColor];
[saveButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal ];
[saveButton setBackgroundImage:[UIImage imageNamed:@"save.png"] forState:UIControlStateNormal];
[saveButton addTarget:self action:@selector(save) forControlEvents:UIControlEventTouchUpInside];
saveButton.hidden = NO;
[self.scrollview addSubview:saveButton];
[saveButton release];

The application crashes when the view appears on screen, and I try and touch any part of the screen. 当视图出现在屏幕上时应用程序崩溃,我尝试触摸屏幕的任何部分。

If I comment out 如果我发表评论

[saveButton release];

the application works perfectly. 该应用程序完美。

I thought the retain count of the button would be incremented once I add it to the scrollview so it would be safe for me to release the button. 我想按钮的保留计数会在我添加到滚动视图后增加,所以我可以安全地释放按钮。

What's going on here? 这里发生了什么? Is adding something to a scrollview not the same as adding it to the main view like below? 是否向滚动视图添加内容与将其添加到主视图(如下所示)不同?

[self.view addSubview:saveButton];

buttonWithType: is a convenience constructor, so it already creates an autoreleased instance, and there is no need to release the object. buttonWithType:是一个方便的构造函数,因此它已经创建了一个自动释放的实例,并且不需要释放该对象。

That means the following line of code is an error: 这意味着以下代码行是一个错误:

[saveButton release];

You should not send release , as the instance is already autoreleased. 您不应发送release ,因为该实例已经自动释放。

Check the UIButton reference for details. 有关详细信息,请查看UIButton参考

UIButton * saveButton = [UIButton buttonWithType:UIButtonTypeCustom];

This according to the Memory Management Rules this code returns an autoreleased object and you don't have to release it when you finish your job with it. 根据内存管理规则,此代码返回一个自动释放的对象,当您使用它完成工作时,您不必释放它。 When you add it as a subview of a view, the view you are adding it to is retaining it and you are not responsible for its memory management. 当您将其添加为视图的子视图时,您要添加它的视图将保留它,并且您不对其内存管理负责。

There is no alloc/init/new used here , so this will be autoreleased. 这里没有使用alloc/init/new ,所以这将是自动释放的。 If you had something like this UIButton *savebutton = [[UIButton alloc]init]; 如果你有这样的东西UIButton *savebutton = [[UIButton alloc]init]; then you would have to use release like : [saveButton release]; 然后你将不得不使用像: [saveButton release];

The problem here is that the 这里的问题是

[UIButton buttonWithType:UIButtonTypeCustom]

method returns an autoreleased object which is only retained by the autorelease pool (which will release the object at the end of current event queue). method返回一个自动释放的对象,该对象仅由自动释放池保留(它将在当前事件队列的末尾释放该对象)。 That means that you have no ownership over it (it's not retained). 这意味着你没有对它的所有权(它没有保留)。 Adding it to scroll view bumps the retain count by one, however you immediately destroy it in the next line by sending a release message. 将其添加到滚动视图会将保留计数加1,但是您可以通过发送释放消息立即在下一行中销毁它。

The correct way to do this would be to remove the release call (and you'll be perfectly clear with memory management). 执行此操作的正确方法是删除释放调用(并且您将完全清楚内存管理)。

You can read more on iOS memory management here . 您可以在此处阅读有关iOS内存管理的更多信息

You hav'nt allocate memory to the button object. 你没有为按钮对象分配内存。 so how can you release it. 所以你怎么能释放它。

UIButton * saveButton = [UIButton buttonWithType:UIButtonTypeCustom];

You called a static method which itself take care of memory management. 您调用了一个静态方法,它本身负责内存管理。 That is the only cause of app crashing. 这是应用程序崩溃的唯一原因。

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

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