My problem is that I am sorting an array with NSSortDescriptor but I have some leaks. I am new to Objective C, though I have a background in C++, however I don't really understand retain, release and autorelease. My, reduced, code is as follows:
-(IBAction)sortByDate
{
NSSortDescriptor *Descriptor = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:Descriptor];
NSArray *sortedArray =[productListe sortedArrayUsingDescriptors:sortDescriptors];
[productListe release];
productListe=nil;
productListe=[[NSMutableArray alloc]initWithArray:sortedArray];
[tableViewProduct reloadData];
NSLog(@"[Descriptor retainCount] =%i",[Descriptor retainCount]); //(return 2)
NSLog(@"[sortDescriptors retainCount] =%i",[sortDescriptors retainCount]); //(return 1)
NSLog(@"[sortedArray retainCount] =%i",[sortedArray retainCount]);//(return 1)
}
In Instruments I get this list of leaks:
NSSortDescriptor 0x80148a0 32 myProject -[myClass sortByDate]
__NSArrayI 0x80148c0 16 myProject -[myClass sortByDate]
__NSArrayReverseEnumerator 0x84079c0 16 UIKit -[UITableView reloadData]
__NSCFArray 0x8015f10 32 Foundation +[NSArray(NSArray) newWithContentsOf:immutable:]
Malloc 32 Bytes 0x800f330 32 Foundation +[NSArray(NSArray) newWithContentsOf:immutable:]
__NSArrayI 0xc914e90 352 Foundation -[NSArray(NSKeyValueSorting) sortedArrayUsingDescriptors:]
Here are my questions :
- If I have some leaks in my application, will Apple reject it from appstore?
- How can I release all objects without a crash.
- Why does reloadData make a leak.
- How can I track the leaks like the Foundation leak in my app.
This is my first application and my first question in a forum so thanks for all your answers.
thank you for answer thats great to know that some one can help . i use instruments and release all in the right place ( i think ;) ) , but when i try to sort my tableview the app crash ( with reloadData and without it). and if u can tell what the percentage (%) in instrument mean ?
now i have no leak in instruments ( except the reload data leak but i think you cant help me in this ) hers is a picture you can see what i have if i use cmd+shift+A ( i dont know her the name of it sorry ) :
i think that " sortDescriptors " is autoreleased but if i dont release it in instruments i have 50% leak ????
think you.