我在我的第一个 iPhone 应用程序上运行了分析,我发现了一些潜在的内存泄漏。该应用程序本身在模拟器上运行良好。我想做正确的事情并清除潜在的内存泄漏,但有些令人费解。也许有人可以在这里帮助我?
提前致谢。
码头。
错误 1)分析器显示“存储在 tempDate 和 tempAns 中的对象的潜在泄漏”
#import "Answer.h"
@implementation Answer
@synthesize answerTiming;
@synthesize xPosition;
@synthesize earlyOrLate;
@synthesize hit;
+ (Answer *) createAnswerWithTiming :(NSDate *)paramTiming andXPosition :(float) xPosition
{
NSDate * tempDate = [[NSDate alloc] initWithTimeInterval:0 sinceDate:paramTiming];
Answer * tempAns = [[Answer alloc] init ];
[tempAns setAnswerTiming:tempDate];
[tempDate release];
[tempAns setXPosition:xPosition];
[tempAns setEarlyOrLate:0];
[tempAns setHit:false];
return tempAns;
}
- (void)dealloc {
[answerTiming release];
[self release];
[super dealloc];
}
@end
错误 2)分析器显示(见下文)
- (void)viewDidLoad
{
[super viewDidLoad];
........
...
UIImage * perfectImage = [UIImage imageNamed: @"perfect.png"];
self.perfectImageView2 = [[UIImageView alloc]initWithImage:perfectImage];
// 方法返回具有 +1 保留计数的目标 C 内容
[self.perfectImageView2 setFrame:CGRectMake(145.0f,
150.0f,
self.perfectImageView2.image.size.width,
self.perfectImageView2.image.size.height)];
self.view.backgroundColor = [UIColor whiteColor];
UIImage * levelUpImage = [UIImage imageNamed:@"levelup.png"];
self.levelUpImageView = [[UIImageView alloc] initWithImage:levelUpImage];
[self.levelUpImageView setFrame:CGRectMake(100.0f,
400.0f,
self.levelUpImageView.image.size.width,
self.levelUpImageView.image.size.height)];
//对象泄漏,分配的对象在此执行路径的后面没有引用,并且保留计数为+1
self.view.backgroundColor = [UIColor whiteColor];
}
错误 3)
- (NSMutableArray *) generateQuestionTapAnswers:(NSString *) answersString withFirstBeat: (NSDate *) firstBeatTime
{
NSArray * notesToDraw = [answersString componentsSeparatedByCharactersInSet: [NSCharacterSet characterSetWithCharactersInString: @" "]];
float noteValueOffset = 0.0;
NSMutableArray * answerArray = [[NSMutableArray alloc] init ];
// 方法返回一个具有 +1 保留计数的目标 C 对象
for (int i=1; i < notesToDraw.count; i++) // i = 0 is the time signature
{
.....
}
return answerArray;
// 对象作为拥有引用返回给调用者(单个保留计数转移给调用者) // 对象泄漏:分配并存储到 answerArray 中的对象是从名称 generateQuestionTapAnswers 不以 copy、mutableCopy 开头的方法返回的