简体   繁体   中英

convert non-ARC to ARC

I was working on non-ARC based project which was very old, and added some new UIViewController s which are based on ARC (can do it by setting -fobjc-arc flag in build phase).

As being mixed use of ARC and non-ARC, sometimes memory leak occurs because forgotten to release somewhere in non-ARC code, and switching from here to there can cause this.

Thus, I have decided to convert non-ARC project to ARC project and do it by following;

Convert non-ARC to ARC project without recreate it

And just removed dealloc, viewDidUnload function contents by removing release or autorelease related things.

After successfully done it, it seems okay to working on but sometimes crashes like message sent to deallocated instance.

I could find what is the reason of that crashes and can be fixed.

What I want to know from here is;

  • When converting, is there any specific guide or rules to do this rather than simple remove of release, autorelease related statements?

Any input will be very appreciated!

Automatic Reference Counting (ARC) is a compiler feature that provides automatic memory management of Objective-C objects.

Refer this url :-

https://developer.apple.com/library/ios/releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html

As mentioned above, in ARC , we need not add release and retain methods since that will be taken care by the compiler. Actually, the underlying process of Objective-C is still the same. It uses the retain and release operations internally making it easier for the developer to code without worrying about these operations, which will reduce both the amount of code written and the possibility of memory leaks .

There was another principle called garbage collection , which is used in Mac OS-X along with MRR, but since its deprecation in OS-X Mountain Lion, it has not been discussed along with MRR. Also, iOS objects never had garbage collection feature. And with ARC , there is no use of garbage collection in OS-X too.

Here is a simple ARC example . Note this won't work on online compiler since it does not support ARC .

#import <Foundation/Foundation.h>

@interface SampleClass:NSObject
- (void)sampleMethod;
@end

@implementation SampleClass

- (void)sampleMethod
{
   NSLog(@"Hello, World! \n");
}

- (void)dealloc 
{
  NSLog(@"Object deallocated");
}

@end

int main()
{
   /* my first program in Objective-C */
   @autoreleasepool{
       SampleClass *sampleClass = [[SampleClass alloc]init];
       [sampleClass sampleMethod];
       sampleClass = nil;
   }
   return 0;
}

get the following output...

demo :- Hello, World!
demo :- Object deallocated

xcode could do the most of the conversion alone. in most cases its enough to remove this statements. you need to check the property declarations @property(nonatomic, weak/strong/copy) .

Here is a litte tutorial: https://objectpartners.com/2013/07/17/converting-an-ios-project-to-use-arc-automatic-reference-counting/

Just follow the things

STEP 1: Go to Project Target and click Build Phases

STEP 2: Click Compile Sources.Where you can see the all the .m files

STEP 3: Double click that,white box will appear with cursor.

STEP 4: give -fno-objc-arc in where the non arc files are.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM