On Nov 12, 2012, at 09:19 , Jerry Krinock <je...@ieee.org> wrote:

> I'm debugging a crash in a large project which evidence indicates is caused 
> by a retain/release imbalance.  The project is written with manual 
> retain/release, not ARC.
> 
> The project is built in Xcode 4.5.2, and when I 'Analyze', I get no warnings 
> pertaining to memory management.  So the problem must be some edge case which 
> is not caught by 'Analyze'.  Further, I think that ARC is built upon 
> 'Analyze', and therefore if I converted this project to ARC, it would still 
> crash.
> 
> Am I correct?

I don't think so. The analyzer is only able to detect retain/release problems 
basic on a *static* analysis of the source code (supplemented perhaps by 
guesses about what certain code patterns suggest about run-time behavior). 
Committing a memory management error is hardly an edge case -- it's 
ridiculously easy to do. In particular, the analyzer doesn't cross file 
boundaries, so a retain in one file that's supposed to be matched by a release 
in another file isn't something the analyzer can detect.

ARC works by propagating correctness. It implements correct memory management 
in each local scope, and follows correctness-preserving rules when 
transitioning into and out of scopes. In theory, if you follow ARC's rules, you 
can't write code with incorrect memory management. So, converting the project 
to ARC may very well solve the problem. In practice, the only real danger 
points are bridging points into and out of ARC control, and a very small number 
of cases where ARC deliberately doesn't preserve 100% correctness.

For example, ARC doesn't fully manage the 'self' pointer, for performance 
reasons. You can write code like this:

                …
                someObjectInAnIvar = …
                [someObjectInAnIvar dontDoThisAtHome];
                …

        - (void) dontDoThisAtHome {
                someObjectInAnIvar = nil;
                …
        }

Assuming there's no other reference keeping the object alive, then it will get 
dealloc'ed in 'dontDoThisAtHome', immediately invalidating 'self'.

This example came up on one of the lists a few months ago, because if the ivar 
happens to be __weak, then counterintuitively ARC throws incaution to the winds 
and safely retains the object for the length of the method invocation.
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to