1

일부 이미지를로드하는 UIScrollView가 있습니다. 가끔 이미지에 효과를 적용하는 데 약간의 시간이 걸리므로 사전로드가 필요하므로 detachNewThreadSelector를 사용하여 다른 스레드에서이 작업을 수행하기로 결정했습니다. . 나는 이것을 위해 gitHub에있는 KTPhotoBrowser를 사용하고있다.iOS detachNewThreadSelector leaking

그래서 기본적으로 나는 기능을 가지고 있습니다.

- (void)setCurrentIndex:(NSNumber *)newIndex 
{ 

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 

    currentIndex_ = [newIndex integerValue]; 

    [self loadPhoto:currentIndex_]; 
    [self loadPhoto:currentIndex_ + 1]; 
    [self loadPhoto:currentIndex_ - 1]; 
    [self unloadPhoto:currentIndex_ + 2]; 
    [self unloadPhoto:currentIndex_ - 2]; 

    [self setTitleWithCurrentPhotoIndex]; 
    [self toggleNavButtons]; 
    [pool release]; 

} 

나는 내가 이것을 실행하면이

[NSThread detachNewThreadSelector:@selector(setCurrentIndex:) toTarget:self withObject:[NSNumber numberWithInt:5]]; 

를 사용하여, 그것은 누출을 던지는 것으로 보인다 호출합니다. loadPhoto 메서드의 코드 주위에 AutoRelease 풀을 넣어야하는지 궁금해지기 시작했습니다. 이 코드에 대해 궁금하신 분은 아래에 포함 시켰습니다.

- (void)loadPhoto:(NSInteger)index 
{ 
    if (index < 0 || index >= photoCount_) { 
     return; 
    } 

    id currentPhotoView = [photoViews_ objectAtIndex:index]; 
    if (NO == [currentPhotoView isKindOfClass:[KTPhotoView class]]) { 
     // Load the photo view. 
     CGRect frame = [self frameForPageAtIndex:index]; 
     KTPhotoView *photoView = [[KTPhotoView alloc] initWithFrame:frame]; 
     [photoView setScroller:self]; 
     [photoView setIndex:index]; 
     [photoView setBackgroundColor:[UIColor clearColor]]; 

     // Set the photo image. 
     if (dataSource_) { 
     if ([dataSource_ respondsToSelector:@selector(imageAtIndex:photoView:)] == NO) { 
      UIImage *image = [dataSource_ imageAtIndex:index]; 
      [photoView setImage:image]; 
     } else { 
     [dataSource_ imageAtIndex:index photoView:photoView]; 
     } 
     } 

     [scrollView_ addSubview:photoView]; 
     [photoViews_ replaceObjectAtIndex:index withObject:photoView]; 
     [photoView release]; 
    } else { 
     // Turn off zooming. 
     [currentPhotoView turnOffZoom]; 
    } 
} 

모든 아이디어는 크게 감사하겠습니다.

+0

누출되는 개체의 종류를 정확하게 알려줄 수 있습니까? –

+0

나는 곧 무슨 일이 일어나는지 보려고기구를 통해 달릴 것입니다. 결과를 다시보고하겠습니다. – jabroni

답변

0

코드는 괜찮은 것 같지만 다른 스레드의 UIKit을 사용하고 있습니다. UIKit 클래스는 응용 프로그램의 주 스레드에서만 사용해야합니다.

UIKit Framework Reference Introduction

+0

답변 해 주셔서 감사합니다. 나는 내가 그것을 더 잘 만드는 곳을보기 위해 이것을 바로 연구 할 것이다. – jabroni

+0

iOS 4.0에서 그 제한이 해제되었습니다. –

+1

이 올바르지 않습니다. iOS4에서 UIKit은 "UIKit의 그래픽 컨텍스트에 그리기"를 제외하고는 스레드로부터 안전하지 않습니다. http://developer.apple.com/library/ios/#releasenotes/General/WhatsNewIniPhoneOS/Articles/iPhoneOS4.html#//apple_ref/doc/uid/TP40009559-SW29 –

0

를 사용하여 다음과 같은

[self performSelectorInBackground:@selector(setCurrentIndex:) withObject:[NSNumber numberWithInt:5]]; 

대신

[NSThread detachNewThreadSelector:@selector(setCurrentIndex:) toTarget:self withObject:[NSNumber numberWithInt:5]]; 

하고 메모리 누수를 제거합니다.