fishinear
fishinear

Reputation: 6346

Instruments Leaks shows non-existent method call

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

Upvotes: 2

Views: 251

Answers (3)

Firoze Lafeer
Firoze Lafeer

Reputation: 17143

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.

Upvotes: 1

Duncan Babbage
Duncan Babbage

Reputation: 20187

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.

Upvotes: 1

ruakh
ruakh

Reputation: 183564

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!)

Upvotes: 0

Related Questions