开发者

How to avoid leaking in iPhone application?

开发者 https://www.devze.com 2023-03-22 05:30 出处:网络
i am getting thing crash log: 2011-07-21 23:18:51.233 iFeel[87679:910b] *** __NSAutoreleaseNoPool(): Object 0x5fbdef0 of class NSURL autoreleased with no pool in place - just leaking

i am getting thing crash log:

2011-07-21 23:18:51.233 iFeel[87679:910b] *** __NSAutoreleaseNoPool(): Object 0x5fbdef0 of class NSURL autoreleased with no pool in place - just leaking
2011-07-21 23:18:51.233 iFeel[87679:910b] *** __NSAutoreleaseNoPool(): Object 0x1462e38 of class NSCFString autorele开发者_如何学JAVAased with no pool in place - just leaking
2011-07-21 23:18:51.233 iFeel[87679:910b] *** __NSAutoreleaseNoPool(): Object 0x1462e38 of class NSCFString autoreleased with no pool in place - just leaking
2011-07-21 23:18:51.233 iFeel[87679:910b] *** __NSAutoreleaseNoPool(): Object 0x5fb32b0 of class NSCFString autoreleased with no pool in place - just leaking
2011-07-21 23:18:51.235 iFeel[87679:910b] *** __NSAutoreleaseNoPool(): Object 0x5fc04e0 of class NSCFString autoreleased with no pool in place - just leaking
2011-07-21 23:18:51.235 iFeel[87679:910b] *** __NSAutoreleaseNoPool(): Object 0x5f98960 of class NSCFString autoreleased with no pool in place - just leaking
2011-07-21 23:18:51.235 iFeel[87679:910b] *** __NSAutoreleaseNoPool(): Object 0x5fa9c70 of class NSCFString autoreleased with no pool in place - just leaking
2011-07-21 23:18:51.550 iFeel[87679:910b] *** __NSAutoreleaseNoPool(): Object 0x5fbfbb0 of class NSHTTPURLResponse autoreleased with no pool in place - just leaking
2011-07-21 23:18:51.550 iFeel[87679:910b] *** __NSAutoreleaseNoPool(): Object 0x5fb5840 of class __NSCFData autoreleased with no pool in place - just leaking
2011-07-21 23:18:51.550 iFeel[87679:910b] *** __NSAutoreleaseNoPool(): Object 0x5fb1400 of class __NSArrayM autoreleased with no pool in place - just leaking
2011-07-21 23:18:51.551 iFeel[87679:910b] *** __NSAutoreleaseNoPool(): Object 0x5f83e70 of class NSCFString autoreleased with no pool in place - just leaking
2011-07-21 23:18:51.551 iFeel[87679:910b] *** __NSAutoreleaseNoPool(): Object 0x5fbd480 of class NSCFString autoreleased with no pool in place - just leaking
2011-07-21 23:18:51.551 iFeel[87679:910b] *** __NSAutoreleaseNoPool(): Object 0x5fb31b0 of class NSPathStore2 autoreleased with no pool in place - just leaking
2011-07-21 23:18:51.551 iFeel[87679:910b] *** __NSAutoreleaseNoPool(): Object 0x5fa9aa0 of class NSPathStore2 autoreleased with no pool in place - just leaking
2011-07-21 23:18:51.551 iFeel[87679:910b] *** __NSAutoreleaseNoPool(): Object 0x5fa6110 of class __NSArrayI autoreleased with no pool in place - just leaking
2011-07-21 23:18:51.552 iFeel[87679:910b] *** __NSAutoreleaseNoPool(): Object 0x5fb9700 of class NSCFString autoreleased with no pool in place - just leaking

Can any body help me to avoid crashing?


It is most likely that you are seeing this because you are executing code on a thread without an autorelease pool. The autorelease pool is heavily used by all of Apple's APIs, so it is important to wrap one around your entire thread. An example of this would be as follows:

- (void)myThreadMethod:(id)anObject {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    NSLog(@"This is some objective-c code...");
    [pool drain];
}

The [pool drain] part is very important. Without that snippet of code, all of the objects that were autoreleased during your threads lifetime will be leaked.


Set up an instance of NSAutoreleasePool. Check out the NSAutoreleasePool Class Reference for details and examples.

You may also want to skim through the Memory Management Programming Guide to see why it's important to set this up and what autorelease actually does.

I also found the discussions on this post helpful: How does the NSAutoreleasePool autorelease pool work?


From Apple documentation link:

The NSAutoreleasePool class is used to support Cocoa’s reference-counted memory management system. An autorelease pool stores objects that are sent a release message when the pool itself is drained.

Important: If you use Automatic Reference Counting (ARC), you cannot use autorelease pools directly. Instead, you use @autoreleasepool blocks. For example, in place of:

 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
// Code benefitting from a local autorelease pool. 
[pool release];

you would write:

 @autoreleasepool {
     // Code benefitting from a local autorelease pool. 
}

@autoreleasepool blocks are more efficient than using an instance of NSAutoreleasePool directly; you can also use them even if you do not use ARC.

0

精彩评论

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