2012-03-21 109 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