How to include non ARC file in your ARC project

1 Comment

Many iOS libraries out there are non ARC. When you include them in your project and try to compile you’ll get errors like:

release is unavailable

To include non ARC files in your ARC project do the following:

Click on your project in the Navigator.

Pick your target.

Click ‘Build Phases’

Click ‘Compile Sources’

Select all the files you want to flag as non ARC

Hit return

Type ‘-fno-objc-arc’

What this basically does is it sets a flag and marks those files as non ARC (so the compiler won’t try to compile them as ARC).

Hit compile and those errors should go away.

Thank you to David Anderson for this tip.

Memory leaks with ARC

6 Comments

ARC (Automatic Reference Counting) is great, but it Cocoa doesn’t set it up for us on background threads, so we can still get memory leaks from application code that calls non-ARC Apple frameworks.

To get the benefits of ARC on any background (or asynchronous) thread calls, wrap the code in an @autorelease pool:

- (void)doSomething {
    [self performSelectorInBackground:@selector(backgroundSomething)];
}

- (void)backgroundSomething {
    @autoreleasepool {
        NSLog(@"Here I am in the background, doing something.");
        myArray = [[NSMutableArray alloc] init];
        // etc.
    }
}

This will autorelease the objects you create here just like they would on the mainthread.

Thanks Stackoverflow for pointing this out.

%d bloggers like this: