【发布时间】:2011-08-27 17:31:18
【问题描述】:
我有一个 5-tab Tab Bar Controller iPad 应用程序。
选项卡之一 (EKG) 导致内存问题。我已经运行了 Instruments,我所能看到的只是 malloc 分配不断增加,大约 12 分钟后,我的所有视图控制器首先获得 didReceiveMemoryWarning 级别 1,然后是级别 2,然后是 SigAbort 0 termination。
应用程序设计的工作方式是当 EKG 选项卡处于活动状态时,每 200 毫秒触发一次setNeedsDisplay,以便在屏幕上绘制(绘制)EKG 样本。当我让应用程序正常运行时,它会在大约 12 分钟后终止。
但是,如果我保留 setNeedsDisplay 并注释掉 drawRect 中的代码,它将运行
永远。我不知道我的“drawRect”中有任何内存分配,但有人在做这些mallocs 下面是我的drawRect 代码:
- (void) drawRect : (CGRect) rect
{
int i, ii, x = 0, xx, y;
fEcgDraw = YES;
CGContextRef context = UIGraphicsGetCurrentContext ();
CGContextSetLineWidth (context, 1);
HH = 376;
if (fEcgErase == YES)
{
CGContextSetStrokeColorWithColor (context,
[UIColor blackColor].CGColor);
/*==============================================================================*/
/* Erase the last screen. */
/*==============================================================================*/
for (i = 0; i < 120; i++)
{
CGContextMoveToPoint (context,
ECGX[x],
(HH - ECGS[x]));
CGContextAddLineToPoint (context, ECGX[(x + 1)],
(HH - ECGS[((x + 1) % 119)]));
x++;
} // end - for (i = 0; i < 120; i++)
CGContextStrokePath (context);
fEcgErase = NO;
} // end - if (fEcgErase == YES)
else if (fECGLOOP)
{
xx = 1;
x = 0;
y = YY;
ii = 0;
for (i =
{
// if (xx == 1)
{
/*==============================================================================*/
/* First erase the prior ECG A/D plot value. */
/*==============================================================================*/
CGContextSetStrokeColorWithColor (context,
[UIColor blackColor].CGColor);
CGContextMoveToPoint (context,
ECGX[x],
(HH - ECGS[x]));
CGContextAddLineToPoint (context,
ECGX[(x + 1)],
(HH - ECGS[((x + 1))]));
CGContextStrokePath (context);
/*==============================================================================*/
/* Now plot the next ECG A/D plot value. */
/*==============================================================================*/
CGContextSetStrokeColorWithColor (context,
[UIColor whiteColor].CGColor);
CGContextMoveToPoint (context,
ECGX[x],
(HH - ECGY[y]));
CGContextAddLineToPoint (context,
ECGX[(x + 1)],
(HH - ECGY[((y + 1) % 119)]));
CGContextStrokePath (context);
ECGS[x] = ECGY[y];
x++;
y = ((y + 1) % 119);
} // end - if (xx == 1)
} // end - for (i = 0; i < 120; i++)
y = ((y + count1) % 119);
YY = y;
count1 = 0;
} // end - if (fEcgErase == YES)
fEcgDraw = NO;
} // end - 'drawRect'
/*===============================END OF FUNCTION================================*/
【问题讨论】:
-
我已经尝试注释掉“drawRect”并留在“setNeedsDisplay”中,这样问题就消失了。我不理解您对 iver 和 gvars 的评论,尤其是 gvars,因为根据定义它们只有一个实例。至于 ivars,我不明白怎么会有多个,因为没有类实例化可以分配更多的 ivars。