【发布时间】:2011-05-13 06:38:35
【问题描述】:
我在 ObjC 中编写的前几个程序运行良好,但杂乱无章,所以这次我想把它做好并使用 MVC。在我尝试通过 VC 将模型中的 NSMutableArray 复制到视图之前,所有部件都可以工作并且已经过测试并且一切都很好。使用了完全相同的格式和代码,并且在程序的另一个方面可以正常工作,但是这个特定的视图使用 drawRect 并且如果我不保留数组就会中断。当我这样做时,它会导致泄漏。为了隔离问题并创建解决方法,我最终直接从 pList 加载数组。它看起来像这样:
@interface HWView : UIView <UIGestureRecognizerDelegate>
{
NSMutableArray *drawStates;
}
@property (nonatomic, retain) NSMutableArray *drawStates;
在.m中
@implementation HWView
@synthesize drawStates;
- (void)awakeFromNib
{
[self HWVReset];
}
-(void)HWVReset
{
NSLog(@"HWVReset:");
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *plistPath = [documentsDirectorystringByAppendingPathComponent: @"PLIST_drawState.plist"];
self.drawStates = [[NSMutableArray arrayWithContentsOfFile:plistPath]retain];
NSLog(@"drawStates:%@",self.drawStates);
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect
{
//draw code
}
- (void)dealloc
{
[self.drawStates release];
[super dealloc];
}
所以它运行但它泄漏。我从以下位置删除保留:“self.drawStates = [[NSMutableArray arrayWithContentsOfFile:plistPath]retain];”它崩溃了。任何帮助表示赞赏。
【问题讨论】:
-
也许你想要强而不是保留?
标签: cocoa-touch memory-leaks nsarray drawrect