如何在objective-c中释放内存?

时间:2015-04-18 07:30:33

标签: ios objective-c memory-management memory-leaks

我正在我的应用程序中开发一个Gps地图概念。我发现我的代码中的内存泄漏我解决了一些内存泄漏最后这里三个内存泄漏即将来临我不知道如何解决这个问题请指导我。这是我的代码库和屏幕截图

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



}

2 个答案:

答案 0 :(得分:2)

对于屏幕截图#2,您似乎并未发布CGImageRef对象。为此,要清理它,你应该使用:

CGImageRelease(image); 

......当您使用CGImageRef完成时。

有关此问题的更多信息(以及它与CFRelease的区别)可以在以下问题中找到:Releasing CGImage (CGImageRef)请注意,即使您使用ARC,在使用任何C时也需要小心基于API的API,特别是如果您将它们与一些Obj-C对象混合在一起。

对于第一个屏幕截图和您发布的代码很难说,因为逻辑非常复杂,但我建议如果您使用ARC,那么我会质疑是否使用&#39; autorelease&#39;在所有初始化程序上是非常必要的。当我尝试使用&#39; autorelease&#39;在一个ARC项目中,它甚至不让我:Xcode给出了消息&#34; ARC禁止显式消息发送&#39; autorelease&#39;。&#34;您可能需要确认您确实已为此项目启用了ARC。

如果它有用,这个问题讨论为您的项目打开ARC:How to enable/disable ARC in an xcode project?


为新添加的屏幕截图编辑

来自Xcode的此屏幕截图的错误消息非常清楚地表明了问题所在。调用&#39; CGColorSpaceCreateDeviceRGB&#39;时,会创建一个您负责显式释放的对象。

如果您查看有关CGColorSpaceCreateDeviceRGB的文档,您会看到文档中的说明&#39;退货&#39;描述:

CGColorSpaceCreateDeviceRGB() Returns Description

因此,您要打电话给CGColorSpaceCreateDeviceRGB&#39;在创建CGContextRef之前,您需要在使用CGColorSpaceRelease完成上下文之后释放它:

CGColorSpaceRef myColorSpaceRef = CGColorSpaceCreateDeviceRGB();

CGContextRef myContext = CGBitmapContextCreate(...);

...

CGContextRelease(myContext);

CGColorSpaceRelease(myColorSpaceRef);

答案 1 :(得分:1)

如果您使用ARC,则无需管理内存。

如果不这样做,只需使用[object release]

强烈建议您在继续之前阅读Managing Memory in Objective-C