1

메모리 누수 문제를 해결하려고하지만 해결책을 찾을 수 없습니다.코코아 터치에서 mutableCopy로 메모리 누수가 발생했습니다.

- (void)refreshData { 

Sn0werSp33dAppDelegate *appDelegate = [[Sn0werSp33dAppDelegate alloc] init]; 
NSFetchRequest *coreDataNewsFetchRequest = [[NSFetchRequest alloc] init]; 
NSEntityDescription *entity = [NSEntityDescription entityForName:@"News" inManagedObjectContext:managedObjectContext]; 

[coreDataNewsFetchRequest setEntity:entity]; 

self.managedObjectContext = appDelegate.managedObjectContext; 
self.newsArray = [[managedObjectContext executeFetchRequest:coreDataNewsFetchRequest error:nil] mutableCopy];//Intruments says that here is the memory leak :(


[appDelegate release]; 
[coreDataNewsFetchRequest release]; 
[entity release]; 
} 

newsArray가있는 NSMutableArray로 내 .H에 선언하고 속성이 있습니다 : 나는 많은 것들을 시도했습니다 @property (nonatomic, retain) NSMutableArray *newsArray;

계기가이 방법에 누수가 말한다 그러나 항상 그 일은 효과가 없었습니다. iPhone SDK 4.2.1 및 Instruments 2.7에서 Xcode 3.2.5를 실행하고 있습니다.

+0

코드를 읽을 수 있도록 포맷해야합니다. –

+0

코드를 약간 포맷 할 수 있습니까? 게다가, 무슨 악기가 말하는거야? mutablecopy는 배열의 완전한 사본을 만들지 않는다는 것을 기억하십시오! – Icky

+0

그것은 단지 하나의 파란색에 빨간색 스택을 보여줍니다. 그리고, 그 코드 형식이 맞습니까? – AppleL1nk

답변

12

당신이 할 때 newsArray 속성에 retain을 추가하기 때문에 something이 유지되는 것을

self.newsArray = something; 

. 방법이 완료 그래서 후

그러나 mutableCopy 또한, 1 씩 증가 유지 카운트 개체를 반환하여 newsArray이있는 당신이 정말로 발견 된 메모리 누수 인, 원하는 것보다 더 높은 수가를 유지합니다.

해결 방법 : 사본을 만들고 그것을 유지

self.newsArray = [[[managedObjectContext executeFetchRequest:coreDataNewsFetchRequest error:nil] mutableCopy] autorelease]; 
+0

초 만에 나를 이길. :-) –

+0

감사합니다 mrueg, 나는 그것이 작동하지 않을 것이라고 생각하고 그래서 그것을 시도하지 않았습니다. 다시 고마워요 : D – AppleL1nk

3

mutableCopyself.newsArray을 할당, 그래서 당신은 당신이 만든 복사본을 해제 할 필요가 어디 라인을 교체합니다. 이에

self.newsArray = [[managedObjectContext executeFetchRequest:coreDataNewsFetchRequest 
                 error:nil] mutableCopy]; 

:이 변경보십시오 앱 위임 클래스의 새로운 객체를 생성, 여담으로

self.newsArray = [[[managedObjectContext executeFetchRequest:coreDataNewsFetchRequest 
                 error:nil] mutableCopy] autorelease]; 

은 조금 특이하고 당신에게 당신이 기대하는 결과를 제공하지 않을 수 있습니다 . 일반적으로 하나의 응용 프로그램 대리인을 인스턴스화합니다 (기본적으로 MainWindow.xib에서이 작업이 수행됩니다).

FooAppDelegate *appDelegate = (FooAppDelegate*)[[UIApplication sharedApplication] delegate]; 
+0

그것은 이미 내게 원하는 결과를 제공하지만, 나는 appDelegate를 공개해야하고 어디에서 릴리스할지 모른다 : – AppleL1nk

관련 문제