开发者

iPhone why am i leaking an object?

开发者 https://www.devze.com 2023-02-15 22:16 出处:网络
I\'m busy with an iPhone app. I\'ve read quite a few things about memory management and i think i\'开发者_开发百科ve got i covered pretty well. But when i\'m running leaks it shows me that i\'m leakin

I'm busy with an iPhone app. I've read quite a few things about memory management and i think i'开发者_开发百科ve got i covered pretty well. But when i'm running leaks it shows me that i'm leaking memory although i think im not.

Its the following code:


UIButton * plus = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
[plus setTitle:@"+" forState:UIControlStateNormal];
[plus addTarget:self action:@selector(plusClicked:) forControlEvents:(UIControlEvents)UIControlEventTouchDown];
plus.frame = CGRectMake(x+35, y-30, 35, 30);
plus.tag = i;
[self.view addSubview: plus];
[plus release];

(the x/y/i vars etc... are defined above and not relevant in this code).

Can anybody explain why i'm leaking the plus object, according to instruments/leaks?


1) There is not enough information to tell for sure, but my guess it's because your controller instance (or it's view) is leaking because after [plus release] the only owner of this button is controller's view.

2) You don't have to retain autoreleased objects to work with them in local scope - they are guaranteed to survive until drain of autorelease pool (which happens at the end of event handling in your case).


This little bit of code retains on the plus button three times. Once in creating the button, once explicitly, and once in addSubView:.

This code also autoreleases the plus button once, and releases it once. (As mentioned in other answers, the explicit retain/release calls are unnecessary, but balanced so don't cause the leak.) Either you don't balance the retain in addSubView: with a release or the autorelease pool is not drained. A possible solution is send a release (or possibly a removeFromSuperview) message to the plus object before releasing the containing view. Another possible solution is to make sure the an autorelease pool is active when this code runs is drained. Just to test, you can add an NSAutoreleasePool before your code, then drain the pool with something like:

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

// your code

[pool release];

If this fixes it, check the structure of your code to figure out why your autorelease pool isn't draining.


Because you are not allocating that Button(plus button). If you are not allocating then you should not release that. Remove [plus release];


Just from the code snippet you posted, it doesn't look like a leak. If you remove the release line as suggested by makboney, you have to also remove the retain of the plus button at the beginning. There's no reason to retain it because it's returned autoreleased and you obviously do not plan to retain the ownership for longer than a couple of lines of code ;)

Try to run the static analyzer (Build->Analyze) maybe it will tell you more about the leak.


remove retain, from UIButton * plus = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
And remove [plus release];

0

精彩评论

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