[LLVMbugs] [Bug 2518] scan-build treats -[NSAutoreleasePool drain] as -[ NSAutoreleasePool release]

bugzilla-daemon at cs.uiuc.edu bugzilla-daemon at cs.uiuc.edu
Wed Jul 9 11:40:17 PDT 2008


http://llvm.org/bugs/show_bug.cgi?id=2518


Ted Kremenek <kremenek at apple.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|                            |INVALID




--- Comment #4 from Ted Kremenek <kremenek at apple.com>  2008-07-09 13:40:16 ---
> I.e. -drain does everything -release does except for actually releasing the
> autorelease pool. 

I talked to the main GC implementor.  He told me that -drain should be treated
exactly as a -release when using autorelease pools in non-GC mode.

If your code works, it is an artifact of the current implementation (which
Apple can change at any time in a future OS release).

The GC guy also said that your code example illustrates a common bug when
people use -drain, and that the static analysis tool actually enforces the
correct specification.

> In short, -drain does not -release the pool. 

I can see why using -drain in this matter would be useful, but it is the case
that -drain releases the pool.

As far as retain counts, auto release pools shouldn't really be viewed as
normal objects.  That's why the -retain method is invalid for
NSAutoreleasePool.  The fact that the retain count appears to stay 1 is also is
an artifact of the implementation.  It should probably be the case that
-retainCount, like -retain, also raises a runtime error for NSAutoreleasePool. 
I'll file this as a feature request to the Apple runtime team.

> If it did so, it’d be worthless.

I think the reason that -drain works this way is to both match scope with
allocation/deallocation and to maintain parity with -release.  It seems to me
that you could easily rewrite your example as:

while(…) {
   NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  …do some stuff…
  [pool drain];
}

Thanks for reporting this bug.  It's good to get important API rules like this
clarified.  I was fully prepared to accept that the tool reported a false
positive; the fact that it didn't means that people commonly have a
misconception about how -drain should be used.


-- 
Configure bugmail: http://llvm.org/bugs/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are on the CC list for the bug.



More information about the llvm-bugs mailing list