OGeek|极客世界-中国程序员成长平台

标题: ios - iPad上的drawRect导致内存问题 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 08:57
标题: ios - iPad上的drawRect导致内存问题

我有一个 5-tab Tab Bar Controller iPad 应用程序。 选项卡之一 (EKG) 导致内存问题。我已经运行了 Instruments,我所能看到的只是 malloc 分配不断增加,大约 12 分钟后,我的所有 View Controller 首先获得 didReceiveMemoryWarning 级别 1,然后是2 级,然后是 SigAbort 0 终止

应用程序的设计工作方式是,当 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================================*/



Best Answer-推荐答案


问题已通过在每个“CGContextStrokePath”之前添加“CGContextClosePath”得到解决,应用现在将永远运行而没有任何“didReceiveMemoryWarning”。

-吉尔·古德里奇

关于ios - iPad上的drawRect导致内存问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7216165/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4