llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

checker false positive: instance variable was retained by a synthesized property but wasn't released in 'dealloc'

Open llvmbot opened this issue 18 years ago • 13 comments

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'

llvmbot avatar Oct 29 '08 16:10 llvmbot

mentioned in issue llvm/llvm-bugzilla-archive#32342

devincoughlin avatar Nov 26 '21 23:11 devincoughlin

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

tkremenek avatar Oct 30 '08 17:10 tkremenek

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.

tkremenek avatar Oct 30 '08 16:10 tkremenek

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.

tkremenek avatar Oct 30 '08 16:10 tkremenek

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;
    

llvmbot avatar Oct 30 '08 16:10 llvmbot

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.

tkremenek avatar Oct 30 '08 16:10 tkremenek

Test case to test ivar release checker A test case which verifies the proposed patch 2159.

llvmbot avatar Oct 30 '08 11:10 llvmbot

Proposed patch to fix false positive This patch has been posted to the cfe-dev mailing list.

llvmbot avatar Oct 30 '08 11:10 llvmbot

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.

llvmbot avatar Oct 29 '08 17:10 llvmbot

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.

tkremenek avatar Oct 29 '08 17:10 tkremenek

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

llvmbot avatar Oct 29 '08 16:10 llvmbot

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?

tkremenek avatar Oct 29 '08 16:10 tkremenek

assigned to @tkremenek

llvmbot avatar Oct 29 '08 16:10 llvmbot