2012-03-21 106 views
1

有人可以告訴我我在做什麼錯嗎?我使用這種方法來繪製圖像。但代碼中的某些內容似乎無法正確釋放,因爲每次我調用此方法時,我的應用程序內存佔用量都會增加。iPhone:內存管理

在這裏,我提供了增加我的記憶力的方法。

-(void)imagemaking 
{ 
UIGraphicsEndImageContext(); 
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 

SBJSON *parser = [[SBJSON alloc] init]; 

statuses = [parser objectWithString:responseString 
           error:nil]; 
[parser release]; 


networkConnection.image = [UIImage imageNamed:@"NetworkTrans1.png"]; 


[updateTimeClock setImage:[UIImage imageNamed:@"GreenClock.png"] forState:UIControlStateNormal]; 

NSArray *segment = [[NSArray alloc]init]; 
segment = [statuses valueForKey:@"Segments"]; 

int linewidth = 3; 

CGSize polyimagesize = CGSizeMake(self.mapView.frame.size.width , self.mapView.frame.size.height); 

UIGraphicsBeginImageContextWithOptions(polyimagesize, NO, 0); 
CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextSetLineWidth(context, linewidth); 

CGContextSetAlpha(context, 0.6);  

for(NSDictionary *route in segment) { 

    NSString *locations = [route valueForKey:@"Locations"]; 

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

     UIColor *color = [UIColor blueColor]; 
     CGContextSetStrokeColorWithColor(context, color.CGColor); 

     for (int i = 0; i <= locations.length - 32; i += 32) { 

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

     CGContextStrokePath(context); 

    }   
}  

madeimage = UIGraphicsGetImageFromCurrentImageContext(); 

UIGraphicsEndImageContext(); 
[responseString release]; 
[segment release]; 

}

按我的調查,因爲CoreGraphics.But的內存佔用增加,我不知道如何減少內存使用或哪個對象是增加內存usage.Please提供指導線。

在此先感謝!

回答

1

您正在分配[狀態valueForKey:@「Segments」];分割和丟失先前由[[NSArray alloc] init]分配的地址;

NSArray *segment = [[NSArray alloc]init]; //Array #1 
segment = [statuses valueForKey:@"Segments"]; //Array #2 (replaces #1) 

最終版本不知道以前分配的內存地址;它只能釋放數組#2。

[segment release]; 

這樣使得第一陣列被泄露的聲明是:

segment = [statuses valueForKey:@"Segments"]; 
+0

我想,首先你應該經歷整個代碼。我在函數的末尾釋放段。我在儀器的幫助下檢查整個10次,我發現在我的應用程序中沒有泄漏。 – Nit 2012-03-21 07:23:30

+0

你正在釋放別的東西。 – jacekmigacz 2012-03-21 07:25:13

+0

@Nit:你釋放'valueForKey:'返回的數組,而不是'alloc'和'init'返回的數組。它們是兩個獨立的數組對象,並且您立即用第二個數組替換第一個,從而泄漏第一個。即使有另外的泄漏(如果有的話,我不能在你顯示的代碼中看到它),jacekmigacz的答案是正確的。 – 2012-03-24 17:05:52

0

採取臨時數組並將其分配給您的段陣列

的NSArray * tempArray = [[NSArray的ALLOC] INIT];

NSArray * segment = tempArray;

segment = [狀態valueForKey:@「Segments」];

[tempArray release];

這將消除由segmentarray引起的內存泄漏。

+0

我已經發布了segment數組,請閱讀完整的帖子。 – Nit 2012-03-22 07:08:16

+0

我知道你已經發布了segment數組,但是試着做tempArray的事情。它肯定會消除內存泄漏。只要試試看 – Shantanu 2012-03-22 07:25:54