Reputation: 7461
Can someone tell me what I am doing wrong here? I use this method to draw image. But something in the code seems to not be released properly because every-time I call a this method and my application memory footprint increases.
Here,I am providing method which increase my memory.
-(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];
}
As per my Investigation,Memory foot print increase because of CoreGraphics.But i don't know how to reduce memory usage or which object is increase the memory usage.Please provide guide-line .
Thanks in advance!
Upvotes: 1
Views: 219
Reputation: 789
You are assigning [statuses valueForKey:@"Segments"]; to segment and loosing address previously allocated by [[NSArray alloc]init];
NSArray *segment = [[NSArray alloc]init]; //Array #1
segment = [statuses valueForKey:@"Segments"]; //Array #2 (replaces #1)
Final release is not aware of previously allocated memory address; it can only release array #2.
[segment release];
So the statement that causes the first array to be leaked is at:
segment = [statuses valueForKey:@"Segments"];
Upvotes: 1
Reputation: 3136
take a temporary array and assign it to your segment array
NSArray *tempArray = [[NSArray alloc]init];
NSArray *segment=tempArray;
segment = [statuses valueForKey:@"Segments"];
[tempArray release];
This will remove the memory leak being caused by segmentarray.
Upvotes: 0