开发者

How to resolve problems when using autorelease on the iPhone?

开发者 https://www.devze.com 2023-03-27 11:13 出处:网络
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

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   myPro开发者_如何学Cject   -[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 :

  1. If I have some leaks in my application, will Apple reject it from appstore?
  2. How can I release all objects without a crash.
  3. Why does reloadData make a leak.
  4. 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 ) :

https://lh6.googleusercontent.com/-cjs_IGsE1Vw/TkccLmyAsGI/AAAAAAAAAEA/YrkE9fUS9XA/Capture%252520d%2525E2%252580%252599e%2525CC%252581cran%2525202011-08-14%252520a%2525CC%252580%25252001.48.43.png

i think that " sortDescriptors " is autoreleased but if i dont release it in instruments i have 50% leak ????

think you.


  1. definitely not. But leaks can cause trouble. Especially if a user uses it for a longer time. Apple might reject your app because leaking memory causes crashes.

  2. You allocate memory and initializes a NSSortDescriptor without releasing it properly after you're done. Rule of thumb is release it as soon as possible if you don't need it anymore. There comes the retainCount of 2, because you added it to an NSArray object which issues a retain message on the Descriptor when you add it.

  3. With luck and your abilities to spot where things go wrong. Seriously, use the Instruments.app with the leak detection monitor and play with your app. You can look into the memory allocations and to the retain / releases including retainCounts of objects allocated. This is a great help.

Look at the stack traces which lead to the leaks and identify your parts and check them for proper retain / releases.

A comment on autorelease objects. autorelease will add an entry to the current NSAutoreleasePool object in place. This pool will drain if you say so and at the beginning of every event loop. See the NSAutoreleasePool Class Reference for further information.

So it typically will not cleanup your precious objects while being in processing a method on your main thread since this blocks your main event loop for a while.

Hope this helps a little bit


  1. Possibly, but just because it gets approved doesn't mean you shouldn't plug the leaks.

  2. Read up on Memory Management for Objective-C (e.g. Memory Management Programming Guide).

  3. You are not releasing an object that gets created when this is called.

  4. Run Build/Build and Analyze or use other debugging tools included with Xcode or use external apps


I can't address your app store question, but I can tell you how to find the leaks: run a static analysis. LLVM will analyze your code and give you a detailed explanation as to why each leak is occurring.

0

精彩评论

暂无评论...
验证码 换一张
取 消