2013-02-17 4 views
0

Я создал основной текстовый вид, и визуально он отлично работает, но я запускаю его в течение 30 минут с большим количеством данных, и он падает. Отладчик начинает говорить «предупреждение о принятой памяти». Я думаю, что взгляды, которые я рисую, обновляюсь на каждой новой строке, входящей в telnet, полностью не оставляют память на перерисовке. код ниже. Первая часть этого кода может быть проблемой. Я обнаружил, что на перерисовке мне пришлось очистить старые взгляды, или это было заложено или нарисовано сверху, и текст исказился. Но это может быть любая из моих переменных, которую я создаю, которые становятся постоянными.core text view memory leak

- (void)buildFrames 
{ 

    for (UIView __strong *view in self.subviews) { 
     [view removeFromSuperview]; 
     view = nil; 
    } 



    frameXOffset = 20; //1 
    frameYOffset = 0; 
    double height=0; 
    double oldHeight = 0; 
    int columnIndex = 0; 
    self.frames=nil; 
    self.frames = [NSMutableArray array]; 
    // self.pagingEnabled = YES; 
    self.delegate = self; 
    CGRect textFrame = CGRectInset(self.bounds, frameXOffset, frameYOffset); 
    // set string 
    int spot =0; 
    if(self.chatLog != nil && self.chatLog != NULL) 
     if(self.chatLog.total > 300) 
      spot = self.chatLog.total - 300; 
    if(spot < 0) 
     spot=0; // double check for thread saftey; 
    int _total = self.chatLog.total; 
    if(_total < 0 || _total > self.chatLog.max) 
     return; 

    if(self.chatLog != nil && self.chatLog != NULL) 
     for(int index = spot; index < _total; index ++) 
     { 

      NSString *theTell = [self.chatLog getChatAt:index]; 
      NSString *chatType = [self.chatLog getTypeAt:index]; 

      if(theTell == nil || theTell == NULL) 
      { [email protected]"nil"; 

       chatType = @"line"; 
      } 
     attString=nil; 
     attString = [[NSMutableAttributedString alloc] initWithString:theTell]; 



      //else 
    // attString = [[NSAttributedString alloc] initWithString:@"Hello core text world"]; 

    CTFontRef font = CTFontCreateWithName(CFSTR("Courier"), self.fontSize, NULL); 
    CFAttributedStringSetAttribute((__bridge CFMutableAttributedStringRef)(attString), CFRangeMake(0, CFAttributedStringGetLength((__bridge CFAttributedStringRef)(attString))), kCTFontAttributeName, font); 
if([chatType isEqual: @"line"]) 
{ 
CFAttributedStringSetAttribute((__bridge CFMutableAttributedStringRef)(attString), CFRangeMake(0, CFAttributedStringGetLength((__bridge CFAttributedStringRef)(attString))), kCTForegroundColorAttributeName, _lineColor);  

} 
else if([chatType isEqual: @"notify"]) 
{ 
CFAttributedStringSetAttribute((__bridge CFMutableAttributedStringRef)(attString), CFRangeMake(0, CFAttributedStringGetLength((__bridge CFAttributedStringRef)(attString))), kCTForegroundColorAttributeName, _notifyColor); 
} 
else if([chatType isEqual: @"tell"]) 
{ 
    CFAttributedStringSetAttribute((__bridge CFMutableAttributedStringRef)(attString), CFRangeMake(0, CFAttributedStringGetLength((__bridge CFAttributedStringRef)(attString))), kCTForegroundColorAttributeName, _tellColor); 
} 
else 
{ 
CFAttributedStringSetAttribute((__bridge CFMutableAttributedStringRef)(attString), CFRangeMake(0, CFAttributedStringGetLength((__bridge CFAttributedStringRef)(attString))), kCTForegroundColorAttributeName, _defaultColor);   
} 

    textFrame = CGRectMake(0, 0, self.bounds.size.width, self.fontSize+4); 
    CGMutablePathRef path = CGPathCreateMutable(); //2 
     CGPathAddRect(path, NULL, textFrame); 

    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)attString); 
    int textPos = 0; //3 

    while (textPos < [attString length]) { //4 


    // CGPoint colOffset = CGPointMake((columnIndex+1)*frameXOffset + columnIndex*(textFrame.size.width), 20); 
     CGPoint colOffset = CGPointMake( 20, (columnIndex+1)*frameYOffset + columnIndex*(textFrame.size.height)); 
     CGRect colRect = CGRectMake(0, 0 , textFrame.size.width-10, textFrame.size.height);// was -40 

     CGMutablePathRef path = CGPathCreateMutable(); 
     CGPathAddRect(path, NULL, colRect); 

     //use the column path 
     CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(textPos, 0), path, NULL); 
     CFRange frameRange = CTFrameGetVisibleStringRange(frame); //get visiblestringrange 
     //create an empty column view 
     CTColumnView *content = [[CTColumnView alloc] initWithFrame: CGRectMake(0, 0, self.contentSize.width, self.contentSize.height)]; 
     content.backgroundColor = [UIColor clearColor]; 
     content.frame = CGRectMake(colOffset.x, colOffset.y, colRect.size.width, colRect.size.height) ; 

     //set the column view contents and add it as subview 
     [content setCTFrame:(__bridge id)frame]; //6 
     [self.frames addObject: (__bridge id)frame]; 
     [self addSubview: content]; 

     //prepare for next frame 
     textPos += frameRange.length; 

     //CFRelease(frame); 
     CFRelease(path); 

     columnIndex++; 
     oldHeight = height; 
     height= [self measureFrameHeight:frame]; 

    } 
     }// end while 
    //set the total width of the scroll view 
    int totalPages = (columnIndex) ; //7 
    // self.contentSize = CGSizeMake(totalPages*self.bounds.size.width, textFrame.size.height); 


    self.contentSize = CGSizeMake(textFrame.size.width, (textFrame.size.height) * (totalPages -1) + height + 40); 

    [self scrollRectToVisible:CGRectMake(0, 0 , textFrame.size.width-10, (textFrame.size.height) * (totalPages -1) + height +40) animated: FALSE]; 



} 

определение класса:

@interface ConsoleView :UIScrollView<UIScrollViewDelegate> 
{ 
    float frameXOffset; 
    float frameYOffset; 

    NSMutableArray *frames; 

} 
@property (strong, nonatomic) NSAttributedString *attString; 
-(void) addNewText:(NSString *) text; 
@property (weak, nonatomic) NSMutableArray *frames;// was reatin not strong 
- (void)buildFrames; 
@property (strong, nonatomic) ChatTextQueue *chatLog; 
@property (nonatomic) int fontSize; 
@property (strong, nonatomic) UIColor *userColor; 
@property (nonatomic) CGColorRef notifyColor; 
@property (nonatomic) CGColorRef lineColor; 
@property (nonatomic) CGColorRef defaultColor; 
@property (nonatomic) CGColorRef tellColor; 

@end 

ответ

1

вы не отпуская некоторые объекты, например, fontframesetter и frame. Используйте команду Analyze в xCode, чтобы найти все утечки памяти в вашем методе buildFrames.

+0

сделал анализ. есть одна проблема с памятью, о которой он говорит. pottential утечка в объект, хранящийся в пути – LanternMike

+0

, теперь освобождает фреймограф и фрейм, и увидит, исправляет ли он его – LanternMike

+0

из документации разработчика яблок по основному тексту, они освобождают фреймсеттер и фрейм, но не шрифт, поэтому теперь не меняют шрифт, и я предполагаю, что я увидим. был над выпуском выпуска шрифта и цвета и имел сбой. теперь я пытаюсь сопоставить страницу с яблоками о том, что выпустить. – LanternMike