OGeek|极客世界-中国程序员成长平台

标题: ios - Instruments Leaks 显示不存在的方法调用 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 20:29
标题: ios - Instruments Leaks 显示不存在的方法调用

要么我根本不懂 Instruments Leaks 工具,要么我快疯了。我已经在我的 iphone 应用程序上运行了该工具,它显示了一些泄漏。如果我理解正确,对于其中一个泄漏,它说它是由我的方法“writeHeading”分配的 NSDate 对象。分配对象的方法是:“dateWithTimeIntervalSinceReferenceDate:”。但是,我的 writeHeading 方法没有使用该方法。事实上,我的整个应用程序中的任何地方都没有使用该方法。

有人知道这里会发生什么吗?

这是writeHeading的代码:

- (void) writeHeadingCLHeading *)heading
{
    if (self.inFlight) {
        [log writeHeading:heading];
    } else {
        IGC_Event *event = [[IGC_Event alloc] init];
        event.code = 'K';
        event.timestamp = heading.timestamp;    
        event.heading = heading;
        [self addEvent:event];
        [event release];
    }
}

这是 Instruments 的截图: enter image description here

这是 IGC_Event 的定义(根据多个响应者的要求):

@interface IGC_Event : NSObject {
    int code;
    CLLocation *location;
    CLHeading *heading;
    NSString *other;
    NSDate *timestamp;
}

@property int code;
@property (nonatomic, retain) CLLocation *location;
@property (nonatomic, retain) CLHeading *heading;
@property (nonatomic, retain) NSString *other;
@property (nonatomic, retain) NSDate *timestamp;

@end


@implementation IGC_Event

@synthesize code;
@synthesize location;
@synthesize heading;
@synthesize other;
@synthesize timestamp;

@end



Best Answer-推荐答案


假设没有 ARC,您需要确保 IGC_Event 对象释放其时间戳和其他可能已保留或复制的引用。

所以在 IGC_Event 中你需要一个这样的 dealloc:

- (void) dealloc {

    [timestamp release];
    [location release];
    [heading release];
    [other release];


    [super dealloc];
}

Leaks 只是告诉您该时间戳对象是在哪里创建的,而不是您应该在哪里发布它。

当然,这可能不是您唯一泄漏的地方,但那里有 4 个潜在的泄漏点。

关于ios - Instruments Leaks 显示不存在的方法调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8106247/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4