2009-09-15 3 views
0

좋아요.이 메모리 누수를 추적하는 데 어려움이 있습니다. 이 스크립트를 실행할 때 어떤 메모리 누수도 보이지 않지만 내 objectalloc은 등반 중입니다. Instruments는 CGBitmapContextCreateImage> create_bitmap_data_provider> malloc을 가리키며, 이는 내 objectalloc의 60 %를 차지합니다.iPhone - UIImage Leak, ObjectAlloc Building

이 코드는 NSTimer를 사용하여 여러 번 호출됩니다.

반환하면 어떻게됩니까?

... 또는 어떻게 UIImage imageWithCGImage가 ObjectAlloc을 빌드하지 않도록 할 수 있습니까?

//I shorten the code because no one responded to another post 
    //Think my ObjectAlloc is building up on that retUIImage that I am returning 
    //**How do I clear that reUIImage after the return?** 

-(UIImage) functionname { 
    //blah blah blah code 
    //blah blah more code 

    UIImage *retUIImage = [UIImage imageWithCGImage:cgImage]; 
      CGImageRelease(cgImage); 

      return retUIImage; 
    } 

답변

1

은 사용이 방법은있는 UIImage를 인스턴스화하고 오토 릴리즈로 설정합니다. 당신이이 정리하려는 경우, 당신은이 중첩 될 수 있다는 풀을 주기적으로

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

주를 비우해야합니다

NSAutoreleasePool *pool1 = [[NSAutoreleasePool alloc] init]; 
NSAutoreleasePool *pool2 = [[NSAutoreleasePool alloc] init]; 
.. 
.. 
.. 
[pool2 release]; 
[pool1 release]; 

일반적인 방법은 루프와 만드는 다른 방법 주변에 이들을 배치하는 것입니다 많은 자동 회수 된 객체.

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
for (Thing *t in things) { 
    [thing doAMethodThatAutoreleasesABunchOfStuff]; 
} 
[pool release] 
+0

이렇게 공개 된 NSAutoreleasePool을 보았습니다 ... [수영장 드레인] 어느 쪽이 맞습니까? [pool drain] 또는 [pool release]? – bbullis21

+0

docs : 참조 계산 환경에서이 메소드는 release와 동일하게 작동합니다. autorelease pool은 retain 할 수 없으므로 (retain을 보라), 따라서 수신기의 할당이 해제된다. 자동 복구 풀이 할당 해제되면 자동 복구 된 모든 객체에 해제 메시지가 전송됩니다. 개체가 동일한 풀에 여러 번 추가되면 풀이 할당 해제되면 추가 될 때마다 해제 메시지가 수신됩니다. 전화에 가비지 수집이 없습니다 – coneybeare

+0

댓글에서 명확하지 않지만 그 설명은 드레인 방식입니다. – coneybeare