ARC Autoreleased with no pool in place

2019-06-14 02:36发布

问题:

I am using ARC in my code and I am getting the error

Object 0x781b8e0 of class __NSCFString autoreleased with no pool in place - just leaking - break on objc_autoreleaseNoPool() to debug

The line it breaks on is

  return UIApplicationMain(argc, argv, nil, NSStringFromClass([HomePageAppDelegate class]));

Since I am using ARC I cannot put an NSAutoReleasePool around it like I usually would. What can I use in order to fix this error?

回答1:

use this on the line where it showing the warning

@autoreleasepool{
}


回答2:

You use the @autoreleasepool construct:

@autoreleasepool {
    // main code here
}

This creates a NSAutoReleasePool with the same scope as the brackets, and it can also be used in MRC code as well. It has the advantages of being cleaned up when exceptions occur, and can easily be used to dispatch threads safely.

To read more, visit this article on Transitioning to ARC Release Notes



回答3:

Create a new test app with ARC enabled. Look at the code in "main.m" to see what Apple recommends.