Instruments Leaks 显示不存在的方法调用

发布于 2024-12-15 04:26:45 字数 1319 浏览 1 评论 0原文

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

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

这是 writeHeading 的代码:

- (void) writeHeading:(CLHeading *)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 的屏幕截图: 在此处输入图像描述

这是 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

Either I don't understand the Instruments Leaks tool at all, or I am going mad. I have run the tool on my iphone app, and it shows a couple of leaks. If I understand it correctly, for one of the leaks, it says that it is an NSDate object allocated by my method "writeHeading". The method that allocates the object is: "dateWithTimeIntervalSinceReferenceDate:". However, my writeHeading method does not use that method. In fact, that method is not used anywhere in my whole application.

Does anybody have an idea what could be going on here?

Here is the code of writeHeading:

- (void) writeHeading:(CLHeading *)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];
    }
}

Here is a screenshot of Instruments:
enter image description here

And here is the definition of IGC_Event (as asked by multiple responders):

@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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

殊姿 2024-12-22 04:26:45

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

因此,在 IGC_Event 中,您需要像这样的 dealloc:

- (void) dealloc {

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


    [super dealloc];
}

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

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

Assuming no ARC, you need to make sure IGC_Event objects release their timestamp and other references that may have been retained or copied.

So in IGC_Event you need a dealloc something like this:

- (void) dealloc {

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


    [super dealloc];
}

Leaks is just telling you where that timestamp object was created, not where you should have released it.

That may not be the only place you are leaking of course, but that's 4 potential leaks right there.

○闲身 2024-12-22 04:26:45

当编译器运行您的代码时,有您直接调用的方法(在您的屏幕截图中,旁边有一个小人),然后是在核心框架中调用的方法。有问题的方法来自这段代码:

event.timestamp = heading.timestamp;

如果您愿意,您可以自己管理此过程:

NSDate *eventTimestamp = heading.timestamp;
event.timestamp = eventTimestamp;

顺便说一句,存储该时间戳是完全多余的,并且使用不必要的内存,因为您还将标题及其所有属性存储在 event.heading 中,因此您可以随时使用 event.heading.timestamp 访问该时间戳。但是,您可能还有其他原因将其单独存储。

When the compiler runs your code, there are the methods directly called by you (which in your screenshot have a little person next to them) and then the methods that are invoked in the core frameworks as a result. The method in question results from this piece of code:

event.timestamp = heading.timestamp;

You could manage this process yourself if you wanted to:

NSDate *eventTimestamp = heading.timestamp;
event.timestamp = eventTimestamp;

Incidentally, storing that timestamp is entirely redundant and uses unnecessary memory, since you also store the heading with all its properties in event.heading so at any time you can access that timestamp with event.heading.timestamp. However, you may have other reasons for storing it separately.

静若繁花 2024-12-22 04:26:45

你有IGC_Event类的实现吗?其 timestamp 属性的设置器是否可能正在调用 dateWithTimeIntevalSinceReferenceDate:? (据我所知,这并不是一件不合理的事情。这将确保它的时间戳是类 NSDate 本身,而不是子类。它还将确保它独立于时间戳被传入。)

(免责声明:我真的不是一个 Objective-C 人。如果这看起来是一个愚蠢的问题,那么它可能就是这样!)

Do you have the implementation of the IGC_Event class? Is it possible that the setter for its timestamp property is calling dateWithTimeIntevalSinceReferenceDate:? (Not an unreasonable thing to do, so far as I can tell. That would ensure that its timestamp is of class NSDate itself, and not a subclass. It would also ensure that it's independent of the timestamp that was passed in.)

(Disclaimer: I'm really not much of an Objective-C-er. If this seems like a stupid question, then it probably is!)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文