ios - 释放分配给 UIBar 的 NSArray

标签 ios objective-c memory-management nsarray

在我的应用程序中,我想制作数组,添加到工具栏然后释放它。但是,当我发布它时,我的应用程序崩溃了。为什么这样...?如何省略它?

UIImage *button1Image = [UIImage imageNamed:@"button1Image.png"];
cameraToolbar = [[UIToolbar alloc] init]; //declared in .h
UIBarButtonItem *button1 = [[ UIBarButtonItem alloc ] initWithTitle: @"qwerty" style: UIBarButtonItemStyleBordered target: self action: @selector(doAction)];
[button1 setImage:button1Image];
//same method to add flexItem and button2Image

NSArray *items = [NSArray arrayWithObjects: button1, flexItem, button2, nil];
[cameraToolbar setItems:items animated:NO];
self.view = cameraToolbar;

[items release]; // here it crashes, why? How to fix?
[button1 release];
[button2 release];
[flexItem release];
[button1Image release]; // here i get "Incorrect decrement of the reference count 
//of an object that is not owned at this point by the caller"
[button2Image release];

最佳答案

[NSArray arrayWithObjects:] 是一个类方法,它返回一个数组,该数组是自动释放的对象。所以你不应该释放它。。 p>

如果您使用诸如 alloc、copy、retain 之类的方式创建对象,那么只有您有责任释放它们。

NSArray *items=[[NSArray alloc]initWithObjects:button1, flexItem, button2, nil];


............



[items release];

像这样的 button1Image 对象。保重

关于ios - 释放分配给 UIBar 的 NSArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7240621/

相关文章:

c++ - 标准字符串应该崩溃但不会

ios - 如何在 Swift 中调用 animateAlongsideTransition?

iphone - 将图像添加到 HTML 字符串 iphone

ios - 在 Objective-C 的 UIWebView 上滚动时 PDF 文件挂起

objective-c - 从模态视图中删除呈现 View

c++ - 为什么 C++ 中存在 delete[] 语法?

c++ - 一个函数使用多少内存?

c# - UIImageView 在触摸时更改 UIImage 然后恢复以前的 UIImage

ios - "Failure to deallocate CLLocationManager on the same runloop as its creation may result in a crash"

iphone - 如何在 iOS 中更改 URL 中的内容?