我正在使用ASIHTTPRequest包装器(AsyncImageLoader)数组来下载UITableView中的单元格的图像.
我在处理ASIHTTPRequests生命周期时遇到问题.如果我发布它们,如果我在尝试加载图像时继续向上和向下滚动,我最终会有一个EXC_BAD_ACCESS.
这是我的包装器的样子.self.request有保留属性,targetCell是我要将图像放入的单元格:
@implementation AsyncImageLoader
- (void)loadImageFromURL:(NSString*)fileName target:(ResultTableViewCell*)targetCell {
// check for cached images, etc
NSURL* url = [NSURL URLWithString:fileName];
[self.request cancel];
self.request = [ASIHTTPRequest requestWithURL:url];
self.request.delegate = self;
}
- (void)startLoading {
[self.request startAsynchronous];
}
- (void)cancel {
[self.request cancel];
self.request = nil;
}
- (void)requestFinished:(ASIHTTPRequest*)requestDone {
// cache image, set cell image...
self.request = nil;
}
- (void)requestFailed:(ASIHTTPRequest*)requestDone {
// handle answer as well
self.request = nil;
}
@end
Run Code Online (Sandbox Code Playgroud)
loadImageFromURL被称为cellForRowAtIndexPath对indexPath.row …
我有一个我作为NSObject创建的类.这个类有许多不同类型和方法的属性等.
当我在我的应用程序中实例化这个类时(比如在主视图控制器中),当我使用它时,我立即发送一个释放调用.即:
MyObject *myObject = [[MyObject alloc] initWithParameters:parms];
[myObject doSomeMethodCall];
[myObject release];
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:当我释放myObject时,它会自动释放我在MyObject .h文件中声明的所有声明的对象,变量等吗?
要么
我是否需要创建一个自定义发布方法来释放所有这些?
我问因为内存管理问题.
谢谢.
我知道实例变量是在dealloc(如下所示)中释放的,但是当它被调用时呢?应用程序关闭时是否释放所有实例变量,或者是否有可接受的方式让它们在不需要时单独解除分配?
- (void)dealloc {
[fred release];
[wilma release];
[barney release];
[betty release];
[super dealloc];
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试构建rss阅读器,当用户完成阅读artical并按回dealloc时不会调用的问题
我得到了retainCount 6和7次!
我有很多自定义面板
当按下后退按钮时,视图是poped并且没有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 …Run Code Online (Sandbox Code Playgroud) 这两个片段是否完成了同样的事情?假设我的界面文件中有三个名为buttonOne,buttonTwo和buttonThree的IBOutlet UIButton:
- (void)dealloc {
for(UIButton* idx in self.view.subviews)
[idx release], idx = nil;
[super dealloc];
}
Run Code Online (Sandbox Code Playgroud)
和
- (void)dealloc {
[buttonOne release], buttonOne = nil;
[buttonTwo release], buttonTwo = nil;
[buttonThree release], buttonThree = nil;
[super dealloc];
}
Run Code Online (Sandbox Code Playgroud)
编辑:
由于ARC有时似乎是iOS中内存管理的替代方案,我更喜欢不使用它,因为a)我觉得我在欺骗b)如果我没有弄错,它只适用于iOS 5设备.
我是iPhone上应用程序开发的新手.根据教程,应该有一个dealloc函数ViewController.m.然而它消失了.还有什么我应该用来释放记忆吗?非常感谢.
我一直在使用来自Raphael Cruzeiro的PDF Annotator的代码,并且发现了许多内存泄漏(ARC已关闭,并将保留以支持旧设备).在修补了大部分内容之后,我对最后一对来说,他们让我难过.因此,在一个叫做类PDFDocument,他的属性了CGPDFPageRef,CGPDFDocument和一个自定义的注释类@synthesize"d.我不得不用释放来消除他的dealloc方法并消除一些悬空指针,除了一个小问题之外它很有效:在大约3个完整的保留 - 释放周期之后,它在@synthesize行崩溃了他的注释对象......我已经从未见过SIGABRT,因为在@synthesize期间发送了一个解除分配的对象,所以自然不知道如何修复它.如果我删除dealloc中的释放代码,它会泄漏,但是如果我将其保留,它就会崩溃.这是PDFDocument类的代码:
//.h
#import <Foundation/Foundation.h>
@class Annotation;
@interface PDFDocument : NSObject {
Annotation *_annotation;
}
- (id)initWithDocument:(NSString *)documentPath;
- (NSInteger) pageCount;
- (void) loadPage:(NSInteger)number;
- (BOOL)save;
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *hash;
@property (readwrite, nonatomic, assign) CGPDFDocumentRef document;
@property (readwrite, nonatomic, assign) CGPDFPageRef page;
@property (nonatomic, retain) NSString *version;
@property (nonatomic, assign) BOOL dirty;
@property (nonatomic, retain) Annotation *annotation;
@end
//.m
#import "PDFDocument.h" …Run Code Online (Sandbox Code Playgroud) 我将保持这个简短明了:我有一个对象的dealloc方法被调用.我还每3秒调用一次NSTimer来记录控制所述对象的当前保留计数.
需要明确的是:我知道NSTimer将保留该对象.即使考虑到这一点,情况仍然没有加起来.
无论如何 - 在这个计时器触发时,对象的保留计数被记录为3.这让我感到困惑有两个原因:
任何帮助是极大的赞赏.谢谢.
编辑:代码:
[NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(logRetainCount) userInfo:nil repeats:YES];
Run Code Online (Sandbox Code Playgroud)
^在viewDidLoad中设置
- (void)logRetainCount
{
NSLog(@"own retain count: %ld", CFGetRetainCount((__bridge CFTypeRef)self));
}
Run Code Online (Sandbox Code Playgroud)
^日志保留计数的方法
- (void)dealloc {
NSLog(@"view controller deallocated");
}
Run Code Online (Sandbox Code Playgroud)
^ dealloc方法在VC中实现,应该被解除分配
和控制台输出:
自己保留计数:5
视图控制器已取消分配
自己保留计数:3
考虑具有强(或弱,相同)NSTimer属性的视图控制器:
__weak __typeof(self) ws = self;
self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:ws selector:@selector(timerTigger:) userInfo:nil repeats:YES];
Run Code Online (Sandbox Code Playgroud)
但是为什么这个视图控制器不会调用dealloc方法,无论是传递strong还是weak引用self?
这是详细的代码:
#import "SecondViewController.h"
@interface SecondViewController ()
@property (nonatomic, weak) NSTimer *timer;
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
__weak __typeof(self) ws = self;
self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:ws selector:@selector(timerTigger:) userInfo:nil repeats:YES];
}
- (void)timerTigger:(id)timer {
NSLog(@"do someting");
}
- (void)dealloc {
NSLog(@"SecondViewController dealloc");
}
Run Code Online (Sandbox Code Playgroud) 我有一个问题,没有在objective-c中释放一个对象.我很确定这是因为它被保留在某处,但我不知道在哪里(检查retainCount应该是0应该返回1).我已多次查看我的代码但未能看到保留它的内容我没有发布.甚至可能是我正在使用的框架中的一个错误.
你会怎么处理这样的事情?我想也许你可以搜索一下内存,看看是什么指向这个对象,让它更容易弄清楚为什么会这样,但我不太清楚如何实现它.也许是另一个解
dealloc ×10
objective-c ×8
ios ×5
iphone ×4
cocoa-touch ×1
memory ×1
nsobject ×1
nstimer ×1
retain ×1
retaincount ×1