-6

I am developing a Gps map concept in my app.Here i find memory leaks in my code i resolve some memory leaks finally here three memory leak is coming i don't know how to resolve this please guide me anyone.This is my code base and Screen shots

https://drive.google.com/file/d/0B14zCKcRh39AWm1QNTFMejNqOGc/view?usp=sharing

https://drive.google.com/file/d/0B14zCKcRh39AcmhyM3R3c1ZrZE0/view?usp=sharing

 -(NSArray *)decodePolyLine: (NSMutableString *)encoded {
        [encoded replaceOccurrencesOfString:@"\\\\" withString:@"\\"
                                    options:NSLiteralSearch
                                      range:NSMakeRange(0, [encoded length])];
        NSInteger len = [encoded length];
        NSInteger index = 0;
        NSMutableArray *array = [[[NSMutableArray alloc] init] autorelease];
        NSInteger lat=0;
        NSInteger lng=0;
        while (index < len) {
            NSInteger b;
            NSInteger shift = 0;
            NSInteger result = 0;
            do {
                b = [encoded characterAtIndex:index++] - 63;
                result |= (b & 0x1f) << shift;
                shift += 5;
            } while (b >= 0x20);
            NSInteger dlat = ((result & 1) ? ~(result >> 1) : (result >> 1));
            lat += dlat;
            shift = 0;
            result = 0;
            do {
                b = [encoded characterAtIndex:index++] - 63;
                result |= (b & 0x1f) << shift;
                shift += 5;
            } while (b >= 0x20);
            NSInteger dlng = ((result & 1) ? ~(result >> 1) : (result >> 1));
            lng += dlng;
            NSNumber *latitude = [[[NSNumber alloc] initWithFloat:lat * 1e-5] autorelease];
            NSNumber *longitude = [[[NSNumber alloc] initWithFloat:lng * 1e-5] autorelease];
            printf("[%f,", [latitude doubleValue]);
            printf("%f]", [longitude doubleValue]);
            CLLocation *loc = [[[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]] autorelease];
            [array addObject:loc];
        }

        return array;
    }

    -(NSArray*) calculateRoutesFrom:(CLLocationCoordinate2D) f to: (CLLocationCoordinate2D) t {
        NSString* saddr = [NSString stringWithFormat:@"%f,%f", f.latitude, f.longitude];
        NSString* daddr = [NSString stringWithFormat:@"%f,%f", t.latitude, t.longitude];

        NSString* apiUrlStr = [NSString stringWithFormat:@"http://maps.google.com/maps?output=dragdir&saddr=%@&daddr=%@", saddr, daddr];
        NSURL* apiUrl = [NSURL URLWithString:apiUrlStr];
        NSLog(@"api url: %@", apiUrl);
        NSError *error;
        NSString *apiResponse = [NSString stringWithContentsOfURL:apiUrl encoding:NSUTF8StringEncoding error:&error];
        NSString* encodedPoints = [apiResponse stringByMatching:@"points:\\\"([^\\\"]*)\\\"" capture:1L] ;

        return [self decodePolyLine:[encodedPoints mutableCopy]];


}




-(void) updateRouteView {
    CGContextRef context =  CGBitmapContextCreate(nil,
                                                  routeView.frame.size.width, 
                                                  routeView.frame.size.height, 
                                                  8, 
                                                  4 * routeView.frame.size.width,
                                                  CGColorSpaceCreateDeviceRGB(),
                                                  (kCGBitmapAlphaInfoMask & kCGImageAlphaPremultipliedLast) | (kCGBitmapByteOrderMask & kCGBitmapByteOrderDefault)) ;


    CGContextSetStrokeColorWithColor(context, lineColor.CGColor);
    CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);
    CGContextSetLineWidth(context, 3.0);

    for(int i = 0; i < routes.count; i++) {
        CLLocation* location = [routes objectAtIndex:i];
        CGPoint point = [mapView convertCoordinate:location.coordinate toPointToView:routeView];

        if(i == 0) {
            CGContextMoveToPoint(context, point.x, routeView.frame.size.height - point.y);
        } else {
            CGContextAddLineToPoint(context, point.x, routeView.frame.size.height - point.y);
        }
    }

    CGContextStrokePath(context);

    CGImageRef image = CGBitmapContextCreateImage(context);
    UIImage* img = [UIImage imageWithCGImage:image];
    CGContextRelease(context);
    routeView.image = img;
   // CGContextRelease(context);



}
bAthi
  • 341
  • 4
  • 20

2 Answers2

3

For screenshot #2 it looks like you're not releasing the CGImageRef object. For this, to clean this up you should use:

CGImageRelease(image); 

...when you're done using the CGImageRef.

More info on this (and how it differs from CFRelease) can be found at this question: Releasing CGImage (CGImageRef) Mind you, even if you're using ARC you need to be careful when using any of the C-based APIs, especially if you're mixing them together with some Obj-C objects.

For the first screenshot and the code you posted it's difficult to say since the logic is quite complex, however I'd suggest if you're using ARC then I'd question if using 'autorelease' on all of your initializers is really necessary. When I try to use 'autorelease' in an ARC project it won't even let me: Xcode gives the message "ARC forbids explicit message send of 'autorelease'." You may want to confirm that you really do have ARC turned on for this project.

In case it is helpful, this question discusses turning on ARC for your project: How to enable/disable ARC in an xcode project?


EDIT for newly added screenshot

The error message from Xcode for this screenshot pretty clearly indicates where the problem is here. When calling 'CGColorSpaceCreateDeviceRGB', this creates an object that you are responsible for releasing explicitly.

If you check the documentation on CGColorSpaceCreateDeviceRGB you'll see that this is also stated in the documentation 'Returns' description:

CGColorSpaceCreateDeviceRGB() Returns Description

Therefore you'll want to call 'CGColorSpaceCreateDeviceRGB' before you create the CGContextRef and you'll need to release it after you're done with the context using CGColorSpaceRelease as such:

CGColorSpaceRef myColorSpaceRef = CGColorSpaceCreateDeviceRGB();

CGContextRef myContext = CGBitmapContextCreate(...);

...

CGContextRelease(myContext);

CGColorSpaceRelease(myColorSpaceRef);
Community
  • 1
  • 1
Derek Lee
  • 3,452
  • 3
  • 30
  • 39
  • First Screen shoot Solution? – bAthi Apr 18 '15 at 09:29
  • I recommend that you confirm that you have ARC added appropriately to your project - convert your project to an ARC project first and then take next steps (note this is not done from Build Phases >> Compile Sources). – Derek Lee Apr 18 '15 at 09:34
  • Yes I agree, if those autorelease messages are not generating a warning then ARC is probably not switched on, at least for this class here if not the entire target. And if you think ARC is on and it's not, well I expect your code will be very leaky indeed. – Jef Apr 18 '15 at 11:09
  • Just now i added one more link see that i forgotten to share these link. – bAthi Apr 18 '15 at 11:15
  • Updated answer to include additional information based on your newly added screenshot. – Derek Lee Apr 18 '15 at 11:36
  • I will try this one but now also it is showing same like that – bAthi Apr 18 '15 at 11:51
1

If you use ARC, you don't need to manage memory.

If you don't, just use [object release].

Strongly suggest you to read Managing Memory in Objective-C before going further.

Enrico Susatyo
  • 19,372
  • 18
  • 95
  • 156