I just used Instrument to check for memory leaks in my app on an iPhone 3G running iOS 3.1.2. I found that there are several leaks displayed in Instruments. The Instruments output is as follows:
Leaked Object # Address Size Responsible Library Responsible Frame
GeneralBlock-16 2 < multiple > 32 UIKit -[UIViewAnimationState animationDidStart:]
GeneralBlock-16 2 < multiple > 32 UIKit -[UIViewAnimationState animationDidStart:]
GeneralBlock-16 0x163be0 16 UIKit -[UITransitionView _didStartTransition]
GeneralBlock-16 0x160730 16 UIKit -[UITableView(UITableViewInternal) _sectionHeaderViewWithFrame:forSectionpaque:reus eViewIfPossible:]
GeneralBlock-16 0x157060 16 UIKit -[UIScrollView(Static) _startTimer:]
GeneralBlock-16 0x148480 16 UIKit -[UIScrollView _endPanWithEvent:]
GeneralBlock-16 0x13d570 16 UIKit -[UINavigationBar pushNavigationItem:]
GeneralBlock-16 0x13c8b0 16 UIKit -[UIScrollView _updatePanWithStartDelta:event:gesture:ignoringDir ectionalScroll:]
GeneralBlock-16 0x132240 16 UIKit -[UINavigationTransitionView transition:fromView:toView:]
GeneralBlock-16 0x126ec0 16 UIKit -[UINavigationBar popNavigationItem]
GeneralBlock-16 0x11ad50 16 UIKit -[UITableViewCell _开发者_JAVA技巧saveOpaqueViewState:]
Because most of the leaked objects come from UIKit (the responsible library reported by Instruments), I'm not sure whether or not I need to clear them, or if it even makes a difference. Are the leaks a serious issue? If I must fix them, how would I do that? I cannot find the trace because the responsible library is not mine.
You should care about them! I currently track down such leaks. There are several possibilities, why they may occure:
1) Designing a UIView in the Interface Builder and initialize this view in a UIViewController (for animations and hiding/showing issues):
You may have defined some IBOutlets (in your .h file), which you might have connected in the Interface Builder to the File's Owner. This IBOutlets should (as far as i know) always be designed as a property (please feel free to correct me, if i'm wrong) and in the dealloc method, don't forget to "nil" it.
e.g: In the viewcontroller header File (i named it MyViewController.h)
@interface MyViewController : UIViewController<UIWebViewDelegate>{
IBOutlet UIWebView* webView;
}
@property (nonatomic, retain) IBOutlet UIWebView* webView;
@end
And in the viewController's .m file:
@implementation
@synthesize webView;
- (void) dealloc {
self.webView = nil; //never forget this, otherwhise it will leak
[super dealloc];
}
@end
2) Designing a UIView in the Interface Builder and subclass this View:
With subclassing i mean, that you can create a subclassed UIView Class and in the Interface Builder you set the class identifier to for example MyView
e.g:
@interface MyView : UIView<UIWebViewDelegate> {
IBOutlet UIWebView* webView;
}
@property (nonatomic, retain) IBOutlet UIWebView* webView;
@end
Same as 1) (IBOutlets shoud be set nil on deallocation)
3) Adding a UIView as subview:
Never forget to remove this view.
e.g: (in my MyViewController, i want to add a subview)
- (void) viewDidLoad {
UIView *aSubView = [[UIView alloc] initWithFrame:CGRectMake(0,0, 90, 90)];
aSubView.tag = 123;
aSubView.backgroundColor = [UIColor blueColor];
[self.view addSubView:aSubView];
[aSubView release];
}
And:
- (void) viewDidUnload {
[[self.view viewWithTag:123] removeFromSuperview]; //remove only aSubView
for (UIView *subview in [self.view subwiews]) { //or remove any subviews
[subview removeFromSuperview];
}
}
Hope it helps!
Br Nic
精彩评论