开发者

iPhone potential leak of an object allocated in line ... and stored into

I am in the middle of debugging an iPhone application involving a split-view application launching the popover on the left and a UINavigatorControl as the detail view on the right. horizontal landscape.

I am having problems resolving the error:

'Potential leak of an object allocated on line (whatever) and stored into 'MasterView'

the line whatever reads as follows:

Ma开发者_如何学PythonsterViewController *masterView = [[MasterViewController alloc] initWithNibName:@"MasterViewController" bundle:nil andArrayOfTags:[self arrayOfButtonTags:self.language]];

and the error pops up at this line (a few lines down the way)

SplitViewDelegate* splitViewDelegate = [[SplitViewDelegate alloc] init];

double-clicking on the memory leak reveals more information (with blue arrows and such).

a few lines above the masterView declaration, the statement

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(defaultsChanged:) name:NSUserDefaultsDidChangeNotification object:nil];

links a blue arrow to the MasterViewController... line with an expanded message of

Method returns an Objective-C object with a +1 retain count (owning reference)

which then leads with a blue arrow to the splitViewDelegate line above.

I am not sure how to clear this error...anyone have any thoughts on this? I think I have an understanding of the code, but I am new to objective c (but have experience in other languages) so walking through this with me a little would be most helpful.


The notification center and split view lines have nothing to do with your problem. They are just the bounds Xcode decided to put on the warning. To fix the error, release masterView when you are done with it. If you put it in a property or display its view in a window, you can safely call release without it being deallocated because it is retained in those processes.


Welcome to the wonderful world of iPhone memory management.

Whenever you alloc a piece of memory you have to make sure you release it. Unless you use autorelease, which would look like:

MasterViewController *masterView = [[[MasterViewController alloc] initWithNibName:@"MasterViewController" bundle:nil andArrayOfTags:[self arrayOfButtonTags:self.language]]autorelease];

and

SplitViewDelegate* splitViewDelegate = [[[SplitViewDelegate alloc] init]autorelease];

Here is a really good overview which explains how to use memory better than I could right now: http://interfacelab.com/objective-c-memory-management-for-lazy-people (ignore the rant about HN at the start)

I hope it helps solve your issue.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜