简体   繁体   English

NSMutableArray和属性泄漏内存

[英]NSMutableArray and property leaking memory

I am in memory-leak cleanup mode on my latest app, and have come across something that I am unable to solve. 我在最新的应用程序上处于内存泄漏清除模式,遇到了无法解决的问题。

the following method has been cleaned up except for 1 nagging issue. 除1个问题外,以下方法已清除。 Instruments tells me that my NSMutableArray called itemsToKeep is leaking memory, at the point that I am creating the object. 仪器告诉我,在我创建对象的那一刻,我的名为itemsToKeep的NSMutableArray正在泄漏内存。 Any ideas on why I am leaking memory would be most appreciated. 关于为什么我要泄漏内存的任何想法将不胜感激。

Here are some notes on retainCounts: entering the method: self.myList has retainCount = 1 exiting the method: self.myList has retainCount = 2 and itemsToKeep has retainCount= 2. I can easily do a [itemsToKeep release] at the end which brings both counts down to 1, but the app crashes after a while (and I think I know why). 以下是关于restCounts的一些说明:进入方法:self.myList的retainCount = 1退出方法:self.myList的retainCount = 2且itemsToKeep的retainCount = 2。两者的计数都减为1,但是一段时间后应用程序崩溃了(我想我知道原因)。

Does anyone know how I can get rid of the memory leak for itemsToKeep? 有谁知道我如何摆脱ItemsToKeep的内存泄漏?

Thanks. 谢谢。

-(void)parsedScores:(BOOL)shouldAdd {

//trim space, tab, newline from both ends
NSString *tmp = self.lblCurrentName.text;
NSString *list = [self trimString:tmp];

NSString *separators = @",";

[self.myList removeAllObjects]; // doesn't impact retain counts

self.myList = (NSMutableArray *)[list componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:separators]]; //this bumps up the self.myList retain count to 2

NSMutableArray *itemsToKeep = [NSMutableArray arrayWithCapacity:30];    

for (NSString *item in self.myList) { 
    NSString *tmpItem = [self trimString:item];
    if (! [self shouldRemoveItem:tmpItem]) {
        [itemsToKeep addObject:tmpItem];
    }   
}

self.myList = itemsToKeep; //makes both variables' retain counts = 2

} }

I can't see a leak in the method you've provided, so I assume it's happening elsewhere. 我看不到您提供的方法中存在泄漏,因此我认为它正在其他地方发生。 You should check if self.myList is retained somewhere else without it being released. 您应该检查self.myList是否保留在其他地方而不释放。

Also, you probably shouldn't be looking at the retain count for debugging purposes. 同样,您可能不应该为了调试目的而查看保留计数。 The retain count can be misleading because it doesn't matter how many times an object is retained as long as it's released an equal amount of times. 保留计数可能会产生误导,因为只要对象被释放相等的时间,它保留多少次都无关紧要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM