Reputation: 12211
I am developing an application where I am drawing in drawRect method of a customClass.
I draw lines in the Graphics context of the View. When I redraw them again and again using setNeedsDisplay, I get memory warnings and my App immediately crashes.
I checked if there were any leaks in my drawRect code. I found nothing. The memory allocation also did not show any major difference.
The problem got fixed once, the iOS device was rebooted. But I am sure the crash will be repeated again. What could be the problem. Have any of you faced similar problems?
The code is below:
- (void)drawRect:(CGRect)rect{
[self drawTheTimeLineHorizontally];
}
- (void) drawTheTimeLineHorizontally {
//Get the current graphics context
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextSaveGState(currentContext);
[UIColorFromRGB(kCalendarTimeLineColor) setStroke];
CGContextSetLineWidth(currentContext, 1);
CGMutablePathRef path = CGPathCreateMutable();
NSArray *hours = [self currentZoomLevelIntervalList];
int numHours = [hours count];
for (int i = 0; i < numHours; ++i) {
CGPathMoveToPoint(path, NULL, 0, (i+kMultiplierTopDailyCalendarTimeline)*offset+2);
[self drawHoursLeftOfLines:[hours objectAtIndex:i] withContext:currentContext withRect:CGRectMake(kOriginXOfTextInTimeLine, (i+kMultiplierTopDailyCalendarTimeline)*offset+(offset/3), kWidthOfTextInTimeLine, offset/3)];
[UIColorFromRGB(kCalendarTimeLineColor) setStroke];
CGPathAddLineToPoint(path, NULL, widthOfDailyCalendar+orginXEventTile, ((i+kMultiplierTopDailyCalendarTimeline)*offset+2));
}
CGPathMoveToPoint(path, NULL, 0, (numHours+kMultiplierTopDailyCalendarTimeline)*offset+2);
CGPathAddLineToPoint(path, NULL, widthOfDailyCalendar+orginXEventTile, (numHours+kMultiplierTopDailyCalendarTimeline)*offset+2);
CGContextAddPath(currentContext, path);
CGContextDrawPath(currentContext, kCGPathStroke);
//CGContextClosePath(currentContext);
CGPathRelease(path);
//Restore the saved context
CGContextRestoreGState(currentContext);
}
- (void) drawHoursLeftOfLines:(NSString*) time withContext:(CGContextRef) context withRect:(CGRect) contextRect {
[UIColorFromRGB(kTimeLineHourTextColor) setStroke];
CGContextSelectFont(context, kTimeLineHourTextFontStyle , kFontSizeTimeLineText, kCGEncodingMacRoman);
CGContextSetCharacterSpacing (context, 1);
CGContextSetTextDrawingMode(context, kCGTextFillStroke);
CGAffineTransform xform = CGAffineTransformMake(
1.0, 0.0,
0.0, -1.0,
0.0, 0.0);
CGContextSetTextMatrix(context, xform);
CGContextShowTextAtPoint(context, contextRect.origin.x, contextRect.origin.y, [time cStringUsingEncoding:NSASCIIStringEncoding], [time length]);
}
UPDATE:
The crash got repeated again in the same flow. This happened after over 8 hours after the device was rebooted. I have not used the App for whole 8 hours. After I rebooted the device, the Application does not crash in that Particular flow at all.
Upvotes: 1
Views: 1619
Reputation: 112857
1) correct the method name getCurrentZoomLevelIntervalList
, perhaps just `currentZoomLevelIntervalList', it just caused confusion to other developers and ARC.
2) Run Analyzer and fix all warnings.
3) Use instruments to check for memory loss due to retained but not leaked memory. The latter is unused memory that is still pointed to. Use Heapshot in the Allocations instrument on Instruments.
For HowTo use Heapshot to find memory creap, see: bbum blog
Basically there method is to run Instruments allocate tool, take a heapshot, run an intuition of your code and another heapshot repeating 3 or 4 times. This will indicate memory that is allocated and not released during the iterations.
To figure out the results disclose to see the individual allocations.
If you need to see where retains, releases and autoreleases occur for an object use instruments:
Run in instruments, in Allocations set "Record reference counts" on on (you have to stop recording to set the option). Cause the picker to run, stop recording, search for there ivar (datePickerView), drill down and you will be able to see where all retains, releases and autoreleases occurred.
Upvotes: 1