2012-03-21 15 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]; 

}私の調査を1として

、理由CoreGraphics.Butのメモリフットプリントの増加私は、メモリの使用量を削減する方法がわからないか、そのオブジェクトがメモリusage.Pleaseガイドラインを提供増加であります。

ありがとうございます!

答えて

1

[statuses 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

+0

あなたは何か他のものをリリースしています。 – jacekmigacz

+0

@Nit: 'valueForKey:'によって返された配列を解放しますが、 'alloc'と' init'によって返される配列は解放しません。それらは2つの別個の配列オブジェクトで、すぐに最初のものを2番目のものに置き換えて、最初のものをリークします。別のリークがあっても(あなたが示したコードでは見えません)、jacekmigaczの答えは正しいです。 –

0

一時配列を取り、あなたのセグメントの配列

にNSArray * tempArrayに割り当て= [[NSArrayのalloc] init];

NSArray * segment = tempArray;

セグメント= [ステータスvalueForKey:@ "セグメント"];

[tempArray release];

これは、segmentarrayによって引き起こされるメモリリークを削除します。

+0

私はすでにセグメント配列をリリースしています。ちょうど完全なポストを読んでください。 – Nit

+0

私はすでにセグメント配列をリリースしているが、tempArrayのことをやってみることを知っている。それは確かにメモリリークを削除します。 – Shantanu