English 中文(简体)
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. Instruments tells me that my NSMutableArray called itemsToKeep is leaking memory, at the point that I am creating the object. 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).

Does anyone know how I can get rid of the memory leak for 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.

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.





相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签