为什么我有内存泄漏?我无法解决这个问题
我在乐器中出现了内存泄漏......我之前已经排序了一些内容,但是这个已经让我难倒了!如果你能帮助,我会非常感激......这是泄漏的方法......它需要一个数据字典,根据它创建一个新的并返回它。我已经评论了泄漏的线条,以及它泄漏的百分比(不知道这是什么意思),我试图通过选择alloc/init/release而不是autorelease来清理一些东西,但是似乎没有任何区别...为什么我有内存泄漏?我无法解决这个问题
- (PetalLayerView *)makePetalLayerWithData:(NSDictionary *)sectionData isZeroIndexed(BOOL)zeroIndexed
{
NSMutableSet *petalsData = [[NSMutableSet alloc] init]; // 7.2%
NSArray *sections = [sectionData objectForKey:@"sections"];
NSNumber *startIndex, *endIndex;
NSDictionary *petalData;
for(int i=0; i<sections.count; i++)
{
startIndex = [sections objectAtIndex:i];
if(i < sections.count - 1)
endIndex = [sections objectAtIndex:i+1];
else
endIndex = [sections objectAtIndex:0];
if(!zeroIndexed)
{
startIndex = [NSNumber numberWithInt:[startIndex intValue]-1]; // 10.2%
endIndex = [NSNumber numberWithInt:[endIndex intValue]-1]; // 10.5%
}
petalData = [[NSDictionary alloc] initWithObjectsAndKeys:startIndex, @"startIndex", endIndex, @"endIndex", nil]; // 64.4%
[petalsData addObject:petalData]; // 7.7%
[petalData release];
}
int maxLength = MAX(self.frame.size.width, self.frame.size.height);
CGRect petalFrame = CGRectMake((self.frame.size.width - maxLength)/2, (self.frame.size.height - maxLength)/2, maxLength, maxLength);
PetalLayerView *petalLayerView = [[[PetalLayerView alloc] initWithFrame:petalFrame] autorelease];
NSString *tagGroupName = [sectionData objectForKey:@"section_name"];
WheelModel *wheelModel = [WheelModel sharedInstance];
if([sectionData objectForKey:@"filtered"])
{
petalLayerView.outlineColor = [wheelModel.tagColors objectForKey:tagGroupName];
}
petalLayerView.petalColor = [wheelModel.petalColors objectForKey:tagGroupName];
petalLayerView.petalsData = petalsData;
[petalsData release];
return petalLayerView;
}
任何帮助非常感谢!谢谢!
:-Joe
后释放了startIndex和endIndex你确定泄漏内存? petalsData被petalLayerView保留(大概),所以所有被分配的数据都不应该消失(即它不是泄漏,它是有意分配的)。我认为,视图本身可能被泄露,但这会超出所提供代码的范围。
谢谢......当你写这个答案时,我发现了这一点。泄漏根本不存在,但在PetalLayerView中(如您所建议的那样)。 PetalLayerView没有在dealloc中释放任何ivars ...我很愚蠢,我没有意识到我必须跟随泄漏到连接的对象。 感谢您的帮助:) – jowie 2010-08-25 21:25:15
你只需要petalData = [[NSDictionary alloc] ...
不,新创建的是'autoreleased',而从数组获得的则不是。见例如[对象所有权策略](http://developer.apple.com/mac/library/documentation/cocoa/conceptual/MemoryMgmt/Articles/mmObjectOwnership.html#//apple_ref/doc/uid/20000043-SW1)。 – 2010-08-25 22:33:27
构建和分析是什么意思?在这样的方法中寻找潜在的泄漏通常是相当不错的。 – theMikeSwan 2010-08-25 21:19:34