此代码泄漏对象的来源是什么? [英] What is the source of the leaky object for this code?

查看:72
本文介绍了此代码泄漏对象的来源是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以帮助重新获得此代码泄漏对象的来源:

Anyone able to help re what is the source of the leaky object for this code:

我的应用程序编译时没有任何ANALYZE问题.当我运行PROFILER并查看内存泄漏"时,我看到泄漏出现了.这些泄漏之一是在我的WeekendEvent对象中.根据下面的说明,仪器中有3个泄漏块"行项目(我在代码中已经指出了这些行所指向的行):

My application compiles without any ANALYZE issues. When I run PROFILER and look at Memory Leaks I see leaks appears. One of these leaks is in my WeekendEvent object. There are 3 Leaked Block row items in instruments per the below (I've noted in the code where these point to):

  • Malloc +1
  • 保留+2
  • 发布+1

问题-我认为这意味着有一个发布版本,但是泄漏的来源是什么.单击工具突出显示的代码段如下.在我看来,这样还可以:

Question - I assume this implies there is a release, however where would this leak be from. The segments of my code that clicking through on instruments highlights is below. To me it seems OK in that:

  1. 我创建的WeekendEvent传递给addWeekendEvent方法后释放
  2. 在addWeekendEvent中,它只是将其添加到NSMutableArray中,因此我认为该安排对其包含的对象进行任何内存管理
  3. 我也确实在dealloc中释放了NSMutableArray

关键源代码位&什么是仪器亮点

// ------Weekends Method (plural)-----
WeekendEvent *weEvent = [[WeekendEvent alloc] init]; // [INSTRUMENTS - 87.5%]
[week addWeekendEvent:weEvent];                      // [INSTRUMENTS - 12.5%]
[weEvent release];                  


//------Weekend *.h ------------
@interface Weekend : NSObject {
    NSMutableArray*     _events;     
}
- (void)addWeekendEvent:(WeekendEvent*)weEvent;
@property (nonatomic, retain) NSMutableArray* events;
@end


//------Weekend *.m -------------
- (void)addWeekendEvent:(WeekendEvent*)weEvent {
    [self.events addObject:weEvent];
}
- (void) dealloc {
    [_events release];
    [super dealloc];
}

编辑:一些其他代码与上面的"week"变量的创建/使用方式有关-因此在Weekends方法中,我发布的代码位于for循环内-带有for循环的代码已显示因此是:

EDIT: Some additional code re how the "week" variable above was created/used - so in the Weekends Method the code I posted was within a for loop - the code with the for loop shown therefore was:

for (Weekend *week in self.items) {
   // do pass "week.start" to some methods (which is an NSDate) - don't think this would be relevant though?
   WeekendEvent *weEvent = [[WeekendEvent alloc] init]; // [INSTRUMENTS - 87.5%]
   [week addWeekendEvent:weEvent];                      // [INSTRUMENTS - 12.5%]
   [weEvent release];   
}               
// Note - self.items I checked is "released" in the dealloc method

编辑2 -确认一下,这是Instruments在其泄漏的对象"列中突出显示的"WeekendEvent"实例.以防万一这还不清楚.

EDIT 2 - Just to confirm, it is an "WeekendEvent" instance that Instruments highlights in it's "leaked objects" column. Just in case this wasn't clear.

编辑3 -重新设置项目变量-关键代码位:

EDIT 3 - Re how I setup the items variable - key code bits are:

@interface Weekends : NSObject {
    NSMutableArray* _items;
}
@property (nonatomic, retain) NSMutableArray* items;

@synthesize items = _items;
- (void) dealloc {
    [_items release];
    [super dealloc];
}

推荐答案

假设其余的Weekend类如下所示,显示的代码中的内存管理是正确的:

The memory management in the code you show is correct, assuming that the rest of your Weekend class looks something like this:

@synthesize events = _events;

- (id)init {
    if ((self = [super init]) == nil) { return nil; }
    _events = [[NSMutableArray alloc] initWithCapacity:0];
    return self;
}

此外,仪器结果与您的所有代码均匹配:

Also, the instruments results match all of your code:

Malloc  +1  ==  WeekendEvent *weEvent = [[WeekendEvent alloc] init];
Retain  +2  ==  [week addWeekendEvent:weEvent];
Release +1  ==  [weEvent release];

基于该逻辑,最可能的候选者是您的week对象未正确释放.您尚未显示解释其创建方式的代码,但我确实注意到您 did 发布的代码是针对Weekend类的.您确定week不是其他类型吗?

Based on that logic, the most likely candidate is that your week object is not properly released. You haven't shown the code that explains how it was created, but I do notice that the code you did post is for a Weekend class. Are you sure week is not of a different type?

这篇关于此代码泄漏对象的来源是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆