2012-04-05 41 views
1

我正在使用以下功能。每當我打電話給它時,它會增加我的記憶使用量。我檢查了所有泄漏。即使在功能結束時我立即釋放對象。在這裏,我提供了我的代碼以供參考。請提供指導。在功能 記憶在使用開始以下功能的內存佔用增加在哪裏?

//存儲器(以字節爲單位):38936576

//內存在的函數(後池漏極) 存儲器中(以字節爲單位)使用結束:39272448

//功能

-(void)parsing:(NSMutableData *)respose 
{ 

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init]; 
report_memory(); 
NSString *responseString = [[NSString alloc] initWithData:respose encoding:NSUTF8StringEncoding]; 

SBJSON *parser = [[SBJSON alloc] init]; 
//NSLog(@"statuses>>>>>"); 
statuses = [parser objectWithString:responseString 
           error:nil]; 

[parser release]; 
//report_memory(); 

refreshCounter = YES; 
__block NSArray *segment =[[NSArray alloc]initWithArray:[statuses valueForKey:@"Segments"]]; 

int mapzoomlevel = [self getZoomLevel]; 
int polylinewidth = 9; 

if(mapzoomlevel == 7) { 
    polylinewidth = 1.5; 
} 
else if(mapzoomlevel == 8) { 
    polylinewidth = 2.5; 
} 
else if(mapzoomlevel ==9) { 
    polylinewidth = 3; 
}  
else if(mapzoomlevel ==10) { 
    polylinewidth = 3.4; 
}  
else if(mapzoomlevel == 11) { 
    polylinewidth = 4; 
}  
else if(mapzoomlevel <= 13) { 
    polylinewidth = 4.3; 
} 
else if (mapzoomlevel == 14) { 
    polylinewidth = 5.4; 
} 
else if(mapzoomlevel== 15) { 
    polylinewidth = 8; 
} 

__block CGContextRef context = NULL; 
CGColorSpaceRef colorSpace; 
//void *   bitmapData; 
int    bitmapByteCount; 
int    bitmapBytesPerRow; 

bitmapBytesPerRow = (self.mapView.frame.size.width * 4); 
bitmapByteCount  = (bitmapBytesPerRow * self.mapView.frame.size.height); 

colorSpace = CGColorSpaceCreateDeviceRGB(); 
// bitmapData = malloc(bitmapByteCount); 

context = CGBitmapContextCreate (NULL, 
           self.mapView.frame.size.width, 
           self.mapView.frame.size.height, 
           8,  // bits per component 
           bitmapBytesPerRow, 
           colorSpace, 
           kCGImageAlphaPremultipliedLast); 
CGContextSetAllowsAntialiasing (context,YES); 
CGColorSpaceRelease(colorSpace); 





CGContextTranslateCTM(context, 0, self.mapView.frame.size.height); 
CGContextScaleCTM(context, 1.0, -1.0); 
CGContextSetLineWidth(context, polylinewidth); 

CGContextSetAlpha(context, 0.6); 
UIColor *color; 

for(NSDictionary *route in segment) { 

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init]; 
    NSString *locations = [route valueForKey:@"Locations"]; 
    double speed = [[route valueForKey:@"Speed"] doubleValue]; 

    if (locations && ([locations length]/16 > 1)) {  


     if (speed <= 20) { 
      color = [UIColor colorWithRed:222/255.0 green:0/255.0 blue:0/255.0 alpha:1.0]; 
     } 
     else if (speed <= 40) { 
      color = [UIColor colorWithRed:253/255.0 green:91/255.0 blue:2/255.0 alpha:1.0]; 
     } 
     else if (speed <= 60) { 
      color = [UIColor colorWithRed:253/255.0 green:145/255.0 blue:4/255.0 alpha:1.0]; 
     } 
     else if (speed <=80) { 
      color = [UIColor colorWithRed:255/255.0 green:212/255.0 blue:4/255.0 alpha:1.0]; 
     } 
     else if (speed >80) { 
      color = [UIColor colorWithRed:42/255.0 green:176/255.0 blue:39/255.0 alpha:1.0]; 
     } 

     CGContextSetStrokeColorWithColor(context, color.CGColor); 

     for (int i = 0; i <= locations.length - 32; i += 32) { 
      NSAutoreleasePool *loc = [[NSAutoreleasePool alloc]init]; 
      CLLocationCoordinate2D coordinates; 
      coordinates.latitude = hexDecode_iPhone([locations substringWithRange:NSMakeRange(i, 16)]); 
      coordinates.longitude = hexDecode_iPhone([locations substringWithRange:NSMakeRange(i+16, 16)]); 

      CGPoint point = [mapView convertCoordinate:coordinates toPointToView:self.mapView]; 

      if (i == 0) 
       CGContextMoveToPoint(context, point.x, point.y); 
      else 
       CGContextAddLineToPoint(context, point.x, point.y); 
      [loc drain]; 
     } 

     CGContextStrokePath(context); 

    } 
    [pool drain]; 
}  
[segment release]; 
[polyImage release]; 
CGImageRef ref = CGBitmapContextCreateImage(context); 
polyImage = [UIImage imageWithCGImage:ref]; 
CGImageRelease(ref); 
CGContextRelease(context); 
[responseString release]; 
    __block NWAnotation *nannotation = [[NWAnotation alloc]initWithImage:polyImage 
              mapView:mapView 
             zoomLevel:ZOOM_LEVEL 
             aRoadFlag:aRoadFlag 
             nRoadFlag:nRoadFlag 
           othersRoadFlag:othersRoadFlag]; 

dispatch_queue_t queue1 = dispatch_queue_create("com.MyApp.AppTask",NULL); 
dispatch_queue_t main = dispatch_get_main_queue(); 
dispatch_async(queue1, 
       ^{ 
        dispatch_async(main, 
            ^{ 
             @try { 
              //NSLog(@"%d",[queue operationCount]); 
             [self showpolyline:nannotation]; 

             } 
             @catch (NSException *exception) { 
              NSLog(@"exception"); 
             } 

            }); 

       }); 

dispatch_release(queue1); 
dispatch_release(main); 
[nwAnotation release]; 
[nannotation release]; 
report_memory(); 
[pool drain]; 
} 

感謝。

+0

爲什麼你沒有報告記憶後,你排水池? – borrrden 2012-04-05 10:52:13

+0

難道你不想錯過nannotation的發佈嗎? – dariaa 2012-04-05 10:52:49

+0

@dariaa:我也做到了......沒有任何區別......我只是忘了在這裏添加。 – Nit 2012-04-05 10:55:07

回答

1

要確定哪些對象導致您的內存增長,請嘗試使用樂器中的分配工具執行堆分析分析。查看this blog post瞭解更多詳情。作爲一個方面說明,你釋放__block變量的註釋的方式是危險的,並且很可能會導致某個點的崩潰。原因是__block變量不會由dispatch_async自動保留,因此在調用[self showpolyline:nannotation]時,nannotation對象可能已被釋放。

+0

謝謝你的答覆。我會加解釋塊變量加5。 – Nit 2012-04-09 01:50:08