Objective-C: an instance variable used while "I" is not set ... but this

I was instructed to clear some Clang errors in the code base. I am very new to developing iPhone and Objective-C, but have found that most of the problems are trivial ... it pushes me, although I'm sure this is awkward.

From the ZAttributedString class:

- (id)initWithAttributedString:(ZAttributedString *)attr { NSParameterAssert(attr != nil); if ((self = [super init])) { _buffer = [attr->_buffer mutableCopy]; _attributes = [[NSMutableArray alloc] initWithArray:attr->_attributes copyItems:YES]; } return self; } 

Klang warning - "An instance variable used when" I "is not set by the result" [super or self] init ...] ", while the dereference of the attr _buffer attribute is highlighted.

If this helps, the warning also mentions that the problem occurs when called from this method:

 - (id)copyWithZone(NSZone *)zone { return [(ZAttributedString *)[ZAttributedString allocWithZone:zone] initWithAttributedString:self]; } 

Can someone explain to me what exactly is there?

TIA!

+6
source share
2 answers

Do not use -> to access instance variables, especially when ivar belongs to another object.

Do it:

 _buffer = [[attr string] mutableCopy]; 

The same goes for this nasty attr->_attributes . Apparently, ZAttributedString exposes attributes` as a property in a private header.


This warning about the compiler seems very optimistic, completely misleading, and most likely completely incorrect in the description. Filing an error so that the explanation is useful.


Please note that @maddy claims that using -> to access instance variables directly in the attr string passed as it acts as a copy constructor is incorrect.

The incoming attr can be an instance of ZAttributedString or an instance of a subclass, or indeed an instance of any class that implements the same interface as ZAttributedString . So you really have to go through accessors to ensure that you are capturing the correct state.

Now, as an implementation detail, ZAttributedString may require that the incoming instance is not a subclass instance of ZAttributedString , but it must use isMemberOfClass: to approve this requirement (and please do not do that).

The only place where direct access to ivar is sometimes used to pull state from another object is to implement copyWithZone: but it is extremely fragile and often leads to violent behavior disruption. In fact, copyWithZone: (outside of the various plist-compatible value classes) is abundant in fragility and the source of many many errors.

+4
source

It seems that you are seeing the same error: "[Error 15092] New: static analyzer false positive: the report instance variable used during 'self' is not set to the result [(super or self)] init" . It has a very similar code to reproduce the error.

If you run this code in Xcode 4.6.3, you can make sure that it gives the same false warning as you.

enter image description here

The error was resolved using a comment :

This is fixed in the trunk, or at least mostly fixed - there are still a few ribs of cases where the alert will fire, but not your project.

(Dave, at the moment, all the main analyzer engineers work at Apple, so there is no real need to duplicate files. LLVM people who don’t work at Apple do not have access to the Apple Clang that comes with Xcode, and this fix did not create Xcode 4.6. You can also get new validation builds from http://clang-analyzer.llvm.org )

As you can see, the bug is fixed, but is still present in Xcode 4.6. Hold on to the next version of Xcode, and the analyzer warning should disappear.

+2
source

Source: https://habr.com/ru/post/950023/


All Articles