views:

32

answers:

1

Hi, am writing an application that plots point on a graph and am using the below method to return an array of these points. I have two views, each displaying different graphs but both have this identical method. Whichever graph is used second causes the application to crash when it reaches the line "NSArray *reading ...". I can't figure out why but am guessing it may have something to do with memory management. Does anyone have any ideas how I can solve this?

-(NSArray*)loadPoints{

    Vehicle *vehicle = [DataModel theDataModel].currentVehicle;

    NSArray *readings = [(NSArray*)vehicle.rVehicleMileage autorelease];

    NSSortDescriptor *descriptor = [[[NSSortDescriptor alloc] initWithKey:@"dteDate" ascending:NO] autorelease];

    readings = [readings sortedArrayUsingDescriptors:[NSArray arrayWithObject:descriptor]];

    return readings;
}

Thanks, William

+4  A: 
NSArray *readings = [(NSArray*)vehicle.rVehicleMileage autorelease];

You should not call -autorelease on some object you don't own. Remove that -autorelease and it should run correctly.

KennyTM
Thanks KennyTM that worked perfectly, I was under the impression that anything that you returned from a method should be autoreleased, but I guess not
wibo
The array produced by `sortedArrayUsingDesriptors:` **is** autoreleased already. If you wanted really wanted to, you could do `return [[readings retain] autorelease];`, but that isn't necessary.
bbum