2012-07-19 26 views
0

我只是想知道如果任何人都可以向我爲什麼下面的行顯示爲具有內儀器內存泄漏解釋:爲什麼下面考慮到內存泄漏?

self.videoEngine = [[VideoEngine alloc] initWithCallbackName:@"IntroFinished"]; 

self.videoEngine = [[VideoEngine alloc] initWithCallbackName:@"MainMovieFinished"]; 

self.timerMap = [NSTimer scheduledTimerWithTimeInterval:fps target:self selector:@selector(updateAnimationTimer) userInfo:nil repeats:YES]; 

NSString *locationName2 = [[NSString alloc] initWithString:[locationName substringFromIndex:test]]; 

是否與不使用初始化時預先設定的NSString的問題嗎?在self.videoEngine和self.timerMap的例子中,它們都具有(非原子,保留)屬性,並在使用前合成。

回答

3

如果你沒有使用弧(其中提到的保留,我認爲你不),那麼這將是你的內存泄漏。

當您分配VideoEngine屬性時,它正在對象上執行另一個保留。你需要添加autorelease,然後結束你的alloc語句。

self.videoEngine = [[[VideoEngine alloc] initWithCallbackName:@"IntroFinished"] autorelease]; 

self.videoEngine = [[[VideoEngine alloc] initWithCallbackName:@"MainMovieFinished"] autorelease]; 

self.timerMap = [NSTimer scheduledTimerWithTimeInterval:fps target:self selector:@selector(updateAnimationTimer) userInfo:nil repeats:YES]; 

NSString *locationName2 = [[NSString alloc] initWithString:[locationName substringFromIndex:test]]; 
+0

謝謝,你能解釋爲什麼我需要autorelease他們,即使我後來釋放他們嗎?我也認爲自己的觀點。是看指針是否等於新的值,如果不釋放它?我現在變得非常困惑...... – 2012-07-19 10:01:44

+1

當你像VideoEngine一樣分配一個對象時,對象的保留計數爲1。而且由於你的屬性被聲明爲(非原子的,保留),它也會在分配對象時增加對象的保留數。 – 2012-07-19 10:11:46

+0

哦好吧,所以如果我按照以下步驟做:VideoEngine * ve = [[VideoEngine alloc] etc],那麼self.videoEngine = ve; ,然後[ve釋放]。那應該沒問題? – 2012-07-19 10:14:32