开发者

Release NSArray assigned to UIBar

In my app i want to make array, add to toolbar and then release it. However when i release it my app crashes. Why so...? What to do to omit it?

UIImage *button1Image = [UIImage imageNamed:@"button1Image.png"];
cameraToolbar = [[UIToolbar alloc] init]; //declared in .h
UIBarButtonItem *button1 = [[ UIBarButtonItem alloc ] initWithTitle开发者_JAVA技巧: @"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:] is a class method which returns an array which is autoreleased object.so u shouldnot release it.

if u use to create object with like alloc,copy,retain then only u have the responsibility to release them.

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


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



[items release];

like this for button1Image object.take care


button1Image is assigned a object which is autoreleased and hence shouldn't be sent a release message. Memory management is based on object reference count and when count becomes zero, the memory will be released. A release message decrements the reference count by one.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜