2013-02-17 6 views
0

코어 텍스트보기를 만들었고 시각적으로 훌륭하게 작동하지만 30 분 동안 많은 데이터가 들어 오면 충돌이 발생합니다. 디버거가 '수신 된 메모리 경고'라고 말하기 시작합니다. 나는 텔넷에서 들어오는 모든 새로운 라인에서 그 새로 고침을 끌어 들이고 있으며, 다시 그리기에 대한 기억을 완전히 떠나지 않을 것이라고 생각한다. 아래 코드를 참조하십시오. 이 코드의 첫 번째 부분은 문제 일 수 있습니다. 내가 다시 그리기에서 나는 오래된보기를 지워야 만했다는 것을 알았거나, 위에 놓이거나 위에 그려 지거나 텍스트가 왜곡되었습니다. 그러나 그것은 내 지속적으로 변해가고있는 변수 중 하나 일 수 있습니다.코어 텍스트보기 메모리 누수

- (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

당신은 예를 font, framesetterframe를 들어, 일부 개체를 해제하지 않습니다. xCode의 Analyze 명령을 사용하여 buildFrames 메서드에서 모든 메모리 누수를 찾습니다.

+0

분석했습니다. 그것이 말하는 한 가지 메모리 문제가 있습니다. podential 누설 경로에 저장된 – LanternMike

+0

글꼴 프레임 세터와 프레임을 발표 할 예정이며, 수정하면 – LanternMike

+0

을 핵심 텍스트의 사과 개발자 문서에서 볼 수 있습니다. 프레임 세터와 프레임을 릴리스하지만 글꼴은 현재 글꼴을 재사용하지 않습니다. 볼 것이다. 글꼴과 색상을 발표하고 충돌을 일으켰다. 지금은 출시 할 페이지에 사과 페이지를 맞추려고합니다. – LanternMike