checker false positive: instance variable was retained by a synthesized property but wasn't released in 'dealloc'
| Bugzilla Link | 2978 |
| Resolution | FIXED |
| Resolved on | Feb 22, 2010 12:45 |
| Version | unspecified |
| OS | MacOS X |
| Reporter | LLVM Bugzilla Contributor |
| CC | @tkremenek |
Extended Description
When a synthesized property
@property (retain) NSString * variable;
is released with
- (void) dealloc { self.variable = nil; [super dealloc]; }
the following error message is generated by the clang checker:
instance variable was retained by a synthesized property but wasn't released in 'dealloc'
mentioned in issue llvm/llvm-bugzilla-archive#32342
Patches applied:
http://lists.cs.uiuc.edu/pipermail/cfe-commits/Week-of-Mon-20081027/008733.html http://lists.cs.uiuc.edu/pipermail/cfe-commits/Week-of-Mon-20081027/008734.html
Looks good. Only thing I would change is...
if(PRE->getProperty() == PD)if(BO->getRHS()->IgnoreParenCasts()->isNullPointerConstant(Ctx))return true;Here we don't need IgnoreParenCasts() such isNullPointerConstant() already handles casts and parentheses. I'll just merge this change in when I apply the patch.
Ok, good point. There's another place where I use extra IgnoreParenCasts():
if (ME->getMethodDecl() == PD->getSetterMethodDecl() &&ME->getNumArgs() == 1 &&ME->getArg(0)->IgnoreParenCasts()->isNullPointerConstant(Ctx))return true;
OK. I'll change that one too.
Created an attachment (id=2160) [details] Test case to test ivar release checker
A test case which verifies the proposed patch 2159.
Test case looks good. The only needed to include it in the Clang test suite is to not have it depend on Foundation.h. Fortunately that's easy:
Replace:
#include <Foundation/Foundation.h>
with:
@interface NSObject @end
I'll merge that change in when I commit the test case.
Looks good. Only thing I would change is...
if(PRE->getProperty() == PD)if(BO->getRHS()->IgnoreParenCasts()->isNullPointerConstant(Ctx))return true;Here we don't need IgnoreParenCasts() such isNullPointerConstant() already handles casts and parentheses. I'll just merge this change in when I apply the patch.
Ok, good point. There's another place where I use extra IgnoreParenCasts():
-
if (ME->getMethodDecl() == PD->getSetterMethodDecl() && -
ME->getNumArgs() == 1 && -
ME->getArg(0)->IgnoreParenCasts()->isNullPointerConstant(Ctx)) -
return true;
Created an attachment (id=2159) [details] Proposed patch to fix false positive
This patch has been posted to the cfe-dev mailing list.
Looks good. Only thing I would change is...
-
if(PRE->getProperty() == PD) -
if(BO->getRHS()->IgnoreParenCasts()->isNullPointerConstant(Ctx)) -
return true;
Here we don't need IgnoreParenCasts() such isNullPointerConstant() already handles casts and parentheses. I'll just merge this change in when I apply the patch.
Test case to test ivar release checker A test case which verifies the proposed patch 2159.
Proposed patch to fix false positive This patch has been posted to the cfe-dev mailing list.
No, that looks right to me. I think the check isn't looking at property assignments within -dealloc. This looks like a false positive.
That's correct. It is indeed a false positive. There should be checks for property assignments.
It was without GC support and iPhone SDK 2.1
I would expect the @synthesize directive to synthesize code like that:
- (void) setVariable:(NSString*)inVariable { if (inVariable != variable) { [variable release]; variable = [inVariable retain]; } }
from: http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/Articles/chapter_5_section_3.html#//apple_ref/doc/uid/TP30001163-CH17-SW14
" Setter Semantics These attributes specify the semantics of a set accessor. They are mutually exclusive.
assign Specifies that the setter uses simple assignment. This is the default.
retain Specifies that retain should be invoked on the object upon assignment. (The default is assign.)
The previous value is sent a release message.
This attribute is valid only for Objective-C object types. (You cannot specify retain for Core Foundation objects—see “Core Foundation.”) "
Thus the variable does get released. Am I wrong here?
Ruotger
It was without GC support and iPhone SDK 2.1
I would expect the @synthesize directive to synthesize code like that:
- (void) setVariable:(NSString*)inVariable { if (inVariable != variable) { [variable release]; variable = [inVariable retain]; } }
from: http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/Articles/chapter_5_section_3.html#//apple_ref/doc/uid/TP30001163-CH17-SW14
" Setter Semantics These attributes specify the semantics of a set accessor. They are mutually exclusive.
assign Specifies that the setter uses simple assignment. This is the default.
retain Specifies that retain should be invoked on the object upon assignment. (The default is assign.)
The previous value is sent a release message.
This attribute is valid only for Objective-C object types. (You cannot specify retain for Core Foundation objects—see “Core Foundation.”) " Thus the variable does get released. Am I wrong here?
No, that looks right to me. I think the check isn't looking at property assignments within -dealloc. This looks like a false positive.
It was without GC support and iPhone SDK 2.1
I would expect the @synthesize directive to synthesize code like that:
- (void) setVariable:(NSString*)inVariable { if (inVariable != variable) { [variable release]; variable = [inVariable retain]; } }
from: http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/Articles/chapter_5_section_3.html#//apple_ref/doc/uid/TP30001163-CH17-SW14
" Setter Semantics These attributes specify the semantics of a set accessor. They are mutually exclusive.
assign Specifies that the setter uses simple assignment. This is the default.
retain Specifies that retain should be invoked on the object upon assignment. (The default is assign.)
The previous value is sent a release message.
This attribute is valid only for Objective-C object types. (You cannot specify retain for Core Foundation objects—see “Core Foundation.”) "
Thus the variable does get released. Am I wrong here?
Ruotger
When a synthesized property
@property (retain) NSString * variable;
is released with
- (void) dealloc { self.variable = nil; [super dealloc]; }
the following error message is generated by the clang checker:
instance variable was retained by a synthesized property but wasn't released in 'dealloc'
Ruotger: Why do you think this is a false positive? Doesn't 'variable' need to be released? The check will only flag a warning if the code is compiled with -fobjc-gc (meaning it is mean to run in BOTH gc and non-gc mode) or isn't compiled with any -fobjc-gc-XXX flag. If the code is compiled with -fobjc-gc-only (meaning it is mean to only run with GC) then this check won't flag a warning.
At the top of the bug report, does it say whether or not the code was compiled with GC support?
assigned to @tkremenek