我的retainCount 增加了?
我正在尝试在这里构建 RSS 阅读器,当用户读完文章并按回时,dealloc 不会调用,并且我得到了保留计数 6 和 6 的问题
。有时7!
我有很多自定义面板,
当按下后退按钮时,视图会弹出,并且没有调用 dealloc?!
.h 文件:
@interface ArticalViewController : UIViewController<UIWebViewDelegate,UIScrollViewDelegate,UIActionSheetDelegate,ArticalBottomPanelDelegate,ArticalContentFetcherDelegate> {
UIWebView * description_;
UIActivityIndicatorView * ind_;
ArticalModel * artical_;
NSString * content;
UIButton * faceBookShareBtn_;
UIBarButtonItem * btnSharePanel_;
CustomTopToolBar * topToolbar_;
ArticalBottomPanel* articalBottomPanel_;
MovingSharePanel * movingSharePanel_;
int fontSize;
BOOL favoStatus;
ArticalContentFetcher *datafetcher_;
}
@property (nonatomic,retain) IBOutlet UIWebView * description;
@property (nonatomic,retain ) IBOutlet UIActivityIndicatorView * ind;
@property (nonatomic,retain) ArticalModel * artical;
@property (nonatomic,retain) IBOutlet UIButton * faceBookShareBtn;
@property (nonatomic,retain) IBOutlet CustomTopToolBar * topToolbar;
@property (nonatomic , retain) IBOutlet ArticalBottomPanel * articalBottomPanel;
@property (nonatomic , retain) IBOutlet MovingSharePanel * movingSharePanel;
@property (nonatomic , retain) ArticalContentFetcher *datafetcher;
-(void) loadArtical:(ArticalModel * )artical;
- (void) loadArticalContentFromInternet;
-(void) changeFavoriteBtnIcon:(BOOL) status;
-(void)backBtnPressed:(id) sender;
-(IBAction)openPostBtnPressed:(id)sender;
@end
.m 文件:
#import "ArticalViewController.h"
@implementation ArticalViewController
@synthesize description=description_;
@synthesize artical=artical_;
@synthesize ind=ind_;
@synthesize faceBookShareBtn=faceBookShareBtn_;
@synthesize topToolbar=topToolbar_;
@synthesize articalBottomPanel=articalBottomPanel_;
@synthesize movingSharePanel=movingSharePanel_;
@synthesize datafetcher=datafetcher_;
- (void)dealloc
{
NSLog(@"ArticalViewController : dealloc");
[description_ release];
[ind_ release];
[artical_ release];
[content release];
[faceBookShareBtn_ release];
[btnSharePanel_ release];
[topToolbar_ release];
[articalBottomPanel_ release];
[movingSharePanel_ release];
[datafetcher_ release];
[super dealloc];
}
#pragma mark -
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
fontSize=100;
[self.articalBottomPanel setDelegate:self];
[self.movingSharePanel setArtical:self.artical];
[self.movingSharePanel setParentView:self];
[self.topToolbar.rightButon setFrame:CGRectMake(260 , 3, 50, 30)];
[self.topToolbar.rightButon addTarget:self action:@selector(backBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.topToolbar.rightButon setImage:[UIImage imageNamed:@"back-button"] forState:UIControlStateNormal];
[self.topToolbar.leftButon setFrame:CGRectMake(10 , 3, 50, 30)];
[self.topToolbar.leftButon setImage:[UIImage imageNamed:@"button-toolbar-post-link"] forState:UIControlStateNormal];
[self.topToolbar.leftButon addTarget:self action:@selector(openPostBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
self.topToolbar.label.text =self.artical.category;
// [label release];
//Navigation Buttons
self.navigationItem.hidesBackButton=YES;
//Check if artical is favorited or not
[self changeFavoriteBtnIcon:[Favorites chechArtical:self.artical]];
[self.description setBackgroundColor:[UIColor clearColor]];
[self.description setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"1px-post-views-background"]]];
if([[self.artical content] length] >1){
NSLog(@"[[self.artical content] length] >1");
[self loadArtical:self.artical];
}else {
NSLog(@"else");
self.datafetcher=[[ArticalContentFetcher alloc ]init ];
[self.datafetcher setArticalContentFetcherDelegate:self];
[NSThread detachNewThreadSelector:@selector(loadArticalContentFromInternet) toTarget:self withObject:nil];
}
}
#pragma mark -
#pragma mark ArticalConnectionFeed Delegate Methods
-(void) articalContentConnectionDoneWithArtical:(ArticalModel *)artical {
NSLog(@"articalContentConnectionDoneWithArtical");
[self loadArtical:artical];
}
-(void) articalContentConnectionFailed{
NSLog(@"articalContentConnectionFailed");
}
#pragma mark -
#pragma mark ArticalBottomPanelDelegate Delegate Methods
-(void) openPanelFired{
NSLog(@"openPanelFired");
[self.movingSharePanel movePanel];
// [self.articalBottomPanel.btnOpenSharePanel setHidden:YES];
}
-(void) fontBtnFired:(int)font{
// NSLog(@"fontBtnFired : %d",font);
if(font==1){
[self.description stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '90%'"];
}else {
[self.description stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '-10%'"];
}
}
-(void) favoBtnFired {
NSLog(@"favoBtnFired");
favoStatus=[Favorites processArtical:self.artical];
[self changeFavoriteBtnIcon:favoStatus];
}
-(void) changeFavoriteBtnIcon:(BOOL) status{
if (status){
[self.articalBottomPanel.btnFavo setImage: [UIImage imageNamed:@"active-star.png"] forState:UIControlStateNormal];
}else {
[self.articalBottomPanel.btnFavo setImage: [UIImage imageNamed:@"star.png"] forState:UIControlStateNormal];
}
}
#pragma mark -
#pragma mark UIWebView Delegate Methods
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if([InternetConnection getInternetStatus]){
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
return YES;
}else {
[InternetConnection ShowNoInternetAlert];
return NO;
}
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[self.ind stopAnimating];
}
#pragma mark -
#pragma mark Class Methods
-(void) loadArtical:(ArticalModel * )artical{
NSLog(@"loadArtical");
[self.artical setContent:[artical content]];
[self.artical setCategory:[artical category]];
NSString * style=[[NSString alloc ] initWithFormat:@"<style> #offline img{display:none;} .wrap{text-align:right;line-height:22px; direction:rtl;} .title{font-size:20px;margin-bottom:5px;} .date{font-size:13px;} .cat{font-size:13px;} </style>"];
if([InternetConnection getInternetStatus])
NSLog(@"[InternetConnection getInternetStatus] : true");
content=[[NSString alloc] initWithFormat:@"%@<div class='wrap' ><div class='title'>%@</div><div class='date'>%@</div><div class='cat'>%@</div>%@</div>",style,[self.artical title],[DateProcessor getInternetDateAndTimeForArticals:self.artical.pubDate],[self.artical category],[self.artical content] ];
[ self.description loadHTMLString:content baseURL:[NSURL URLWithString:@""]];
[style release];
}
- (void) loadArticalContentFromInternet{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[self.datafetcher loadArticlContentWithID:self.artical.ID];
[[NSRunLoop currentRunLoop] run ];
[pool release];
}
-(void)backBtnPressed:(id) sender{
NSLog(@"backBtnPressed");
[self.navigationController popViewControllerAnimated:YES];
}
#pragma mark -
#pragma mark IBActions
-(IBAction)openPostBtnPressed:(id)sender{
if([InternetConnection getInternetStatus]){
[InternetConnection openExternalUrl:self.artical.link];
}else{
[InternetConnection ShowNoInternetAlert];
}
}
@end
添加:
当我调用我使用的 Artical 时
avc=[[ArticalViewController alloc]initWithNibName:@"ArticalViewController" bundle:nil];
avc.artical=[self.feeds objectAtIndex:indexPath.row];
[self.navCon pushViewController:avc animated:YES];
[avc release];
am trying here to build rss reader , the problem that when user finish read artical and press back the dealloc don't called
and i got retainCount 6 & some times 7 !!
i have lots of customized panels
when back button pressed the view is poped and no dealloc called ?!
.h file :
@interface ArticalViewController : UIViewController<UIWebViewDelegate,UIScrollViewDelegate,UIActionSheetDelegate,ArticalBottomPanelDelegate,ArticalContentFetcherDelegate> {
UIWebView * description_;
UIActivityIndicatorView * ind_;
ArticalModel * artical_;
NSString * content;
UIButton * faceBookShareBtn_;
UIBarButtonItem * btnSharePanel_;
CustomTopToolBar * topToolbar_;
ArticalBottomPanel* articalBottomPanel_;
MovingSharePanel * movingSharePanel_;
int fontSize;
BOOL favoStatus;
ArticalContentFetcher *datafetcher_;
}
@property (nonatomic,retain) IBOutlet UIWebView * description;
@property (nonatomic,retain ) IBOutlet UIActivityIndicatorView * ind;
@property (nonatomic,retain) ArticalModel * artical;
@property (nonatomic,retain) IBOutlet UIButton * faceBookShareBtn;
@property (nonatomic,retain) IBOutlet CustomTopToolBar * topToolbar;
@property (nonatomic , retain) IBOutlet ArticalBottomPanel * articalBottomPanel;
@property (nonatomic , retain) IBOutlet MovingSharePanel * movingSharePanel;
@property (nonatomic , retain) ArticalContentFetcher *datafetcher;
-(void) loadArtical:(ArticalModel * )artical;
- (void) loadArticalContentFromInternet;
-(void) changeFavoriteBtnIcon:(BOOL) status;
-(void)backBtnPressed:(id) sender;
-(IBAction)openPostBtnPressed:(id)sender;
@end
.m File :
#import "ArticalViewController.h"
@implementation ArticalViewController
@synthesize description=description_;
@synthesize artical=artical_;
@synthesize ind=ind_;
@synthesize faceBookShareBtn=faceBookShareBtn_;
@synthesize topToolbar=topToolbar_;
@synthesize articalBottomPanel=articalBottomPanel_;
@synthesize movingSharePanel=movingSharePanel_;
@synthesize datafetcher=datafetcher_;
- (void)dealloc
{
NSLog(@"ArticalViewController : dealloc");
[description_ release];
[ind_ release];
[artical_ release];
[content release];
[faceBookShareBtn_ release];
[btnSharePanel_ release];
[topToolbar_ release];
[articalBottomPanel_ release];
[movingSharePanel_ release];
[datafetcher_ release];
[super dealloc];
}
#pragma mark -
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
fontSize=100;
[self.articalBottomPanel setDelegate:self];
[self.movingSharePanel setArtical:self.artical];
[self.movingSharePanel setParentView:self];
[self.topToolbar.rightButon setFrame:CGRectMake(260 , 3, 50, 30)];
[self.topToolbar.rightButon addTarget:self action:@selector(backBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.topToolbar.rightButon setImage:[UIImage imageNamed:@"back-button"] forState:UIControlStateNormal];
[self.topToolbar.leftButon setFrame:CGRectMake(10 , 3, 50, 30)];
[self.topToolbar.leftButon setImage:[UIImage imageNamed:@"button-toolbar-post-link"] forState:UIControlStateNormal];
[self.topToolbar.leftButon addTarget:self action:@selector(openPostBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
self.topToolbar.label.text =self.artical.category;
// [label release];
//Navigation Buttons
self.navigationItem.hidesBackButton=YES;
//Check if artical is favorited or not
[self changeFavoriteBtnIcon:[Favorites chechArtical:self.artical]];
[self.description setBackgroundColor:[UIColor clearColor]];
[self.description setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"1px-post-views-background"]]];
if([[self.artical content] length] >1){
NSLog(@"[[self.artical content] length] >1");
[self loadArtical:self.artical];
}else {
NSLog(@"else");
self.datafetcher=[[ArticalContentFetcher alloc ]init ];
[self.datafetcher setArticalContentFetcherDelegate:self];
[NSThread detachNewThreadSelector:@selector(loadArticalContentFromInternet) toTarget:self withObject:nil];
}
}
#pragma mark -
#pragma mark ArticalConnectionFeed Delegate Methods
-(void) articalContentConnectionDoneWithArtical:(ArticalModel *)artical {
NSLog(@"articalContentConnectionDoneWithArtical");
[self loadArtical:artical];
}
-(void) articalContentConnectionFailed{
NSLog(@"articalContentConnectionFailed");
}
#pragma mark -
#pragma mark ArticalBottomPanelDelegate Delegate Methods
-(void) openPanelFired{
NSLog(@"openPanelFired");
[self.movingSharePanel movePanel];
// [self.articalBottomPanel.btnOpenSharePanel setHidden:YES];
}
-(void) fontBtnFired:(int)font{
// NSLog(@"fontBtnFired : %d",font);
if(font==1){
[self.description stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '90%'"];
}else {
[self.description stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '-10%'"];
}
}
-(void) favoBtnFired {
NSLog(@"favoBtnFired");
favoStatus=[Favorites processArtical:self.artical];
[self changeFavoriteBtnIcon:favoStatus];
}
-(void) changeFavoriteBtnIcon:(BOOL) status{
if (status){
[self.articalBottomPanel.btnFavo setImage: [UIImage imageNamed:@"active-star.png"] forState:UIControlStateNormal];
}else {
[self.articalBottomPanel.btnFavo setImage: [UIImage imageNamed:@"star.png"] forState:UIControlStateNormal];
}
}
#pragma mark -
#pragma mark UIWebView Delegate Methods
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if([InternetConnection getInternetStatus]){
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
return YES;
}else {
[InternetConnection ShowNoInternetAlert];
return NO;
}
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[self.ind stopAnimating];
}
#pragma mark -
#pragma mark Class Methods
-(void) loadArtical:(ArticalModel * )artical{
NSLog(@"loadArtical");
[self.artical setContent:[artical content]];
[self.artical setCategory:[artical category]];
NSString * style=[[NSString alloc ] initWithFormat:@"<style> #offline img{display:none;} .wrap{text-align:right;line-height:22px; direction:rtl;} .title{font-size:20px;margin-bottom:5px;} .date{font-size:13px;} .cat{font-size:13px;} </style>"];
if([InternetConnection getInternetStatus])
NSLog(@"[InternetConnection getInternetStatus] : true");
content=[[NSString alloc] initWithFormat:@"%@<div class='wrap' ><div class='title'>%@</div><div class='date'>%@</div><div class='cat'>%@</div>%@</div>",style,[self.artical title],[DateProcessor getInternetDateAndTimeForArticals:self.artical.pubDate],[self.artical category],[self.artical content] ];
[ self.description loadHTMLString:content baseURL:[NSURL URLWithString:@""]];
[style release];
}
- (void) loadArticalContentFromInternet{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[self.datafetcher loadArticlContentWithID:self.artical.ID];
[[NSRunLoop currentRunLoop] run ];
[pool release];
}
-(void)backBtnPressed:(id) sender{
NSLog(@"backBtnPressed");
[self.navigationController popViewControllerAnimated:YES];
}
#pragma mark -
#pragma mark IBActions
-(IBAction)openPostBtnPressed:(id)sender{
if([InternetConnection getInternetStatus]){
[InternetConnection openExternalUrl:self.artical.link];
}else{
[InternetConnection ShowNoInternetAlert];
}
}
@end
Addition :
when i was invoking the Artical i used
avc=[[ArticalViewController alloc]initWithNibName:@"ArticalViewController" bundle:nil];
avc.artical=[self.feeds objectAtIndex:indexPath.row];
[self.navCon pushViewController:avc animated:YES];
[avc release];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看起来很奇怪,
retainCount
对于计算保留量根本没有用处。除了调用[myObject keep]
或[[MyClass alloc] init]
等之外,事物也会因其他原因而被保留。最好忽略
retainCount
并了解内存管理规则。retainCount
会让你更加困惑。如果您仅针对 iOS 5 进行开发,最好忘记内存管理并使用 ARC 。Strange as it may seem,
retainCount
is not useful at all for counting your retains. Things get retained for other reasons than you calling[myObject retain]
or[[MyClass alloc] init]
etc.It's better to ignore
retainCount
and learn the memory management rules.retainCount
will make you more confused. If you're developing only for iOS 5, it's better to forget about memory management and use ARC.NSThread detachNewThreadSelector:toTarget:withObject:
保留其目标,在本例中为self
。另外, self 是这里一些事情的委托人,通常您不想保留委托人,所以如果您编写了这些协议,请检查您是否没有这样做。NSThread detachNewThreadSelector:toTarget:withObject:
retains its target, which in this case isself
. Also, self is the delegate of a few things here, usually you don't want to retain delegates so if you've written those protocols check that you are not doing that.我发现跟踪保留计数和丢失保留-释放对的最佳方法是使用Instruments。点击配置文件 (Cmd ⌘+I) 并选择Leaks模板。即使不会自动发现泄漏,也会记录保留更改,以便您可以手动跟踪其他保留。为此,请在选择分配工具时在对象摘要中找到您的类名称。如果找不到它,则意味着所有实例都已释放。否则,单击选择类名称时出现的箭头:
您将看到您班级的所有活动实例:
如果您认为某些实例应该已经存在解除分配,选择一个并单击对象地址旁边出现的箭头。现在,您应该看到使用执行此操作的方法名称对此对象调用的任何保留或释放:
RefCt 列显示操作后的保留计数被调用,当您双击任何保留/释放时,仪器将向您显示执行此操作的代码行:
如您所见,对象被添加到数组中并且永远不会从数组中删除。
根据我的经验,这是查找 Leaks 工具无法自动检测到的内存泄漏的最快、最简单的方法。我认为另一个好的做法是查看对象摘要中的#Living,以确保活动实例的数量完全符合您的预期。
I found that the best way to trace retain counts and missing retain-release pairs is using Instruments. Hit Profile (Cmd ⌘+I) and choose Leaks template. Even if leaks will not be discovered automatically, retain changes are logged so you can manually trace additional retains. To do that, find your class name in Object Summary when selected Allocations instrument. If you can't find it, this means that all instances were deallocated. Otherwise click on the arrow that appears when you select class name:
You will see all living instances of your class:
If you suppose that there are some instances that should be already deallocated, select one and click on the arrow that appeared next to object address. Now you should see any retain or release that was invoked on this object with method name that was performing this action:
RefCt column shows retainCount after action was invoked, and when you double click on any retain/release, instruments will show you line of code where this was performed:
As you see, object is added to an array and never removed from it.
In my experience this is the fastest and easiest way to find memory leaks that Leaks instrument would not automatically detect. I think another good practice is to look at #Living in Object Summary to ensure that number of living instances are exactly as you'd expect.
您没有理由关心保留计数是多少。只要确保平衡保留和释放即可。
You have no reason to care what the retain count is. Just make sure you balance your retains and releases.