• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: What does the CLANG_WARN_OBJC_RECEIVER_WEAK warning protect us from?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: What does the CLANG_WARN_OBJC_RECEIVER_WEAK warning protect us from?


  • Subject: Re: What does the CLANG_WARN_OBJC_RECEIVER_WEAK warning protect us from?
  • From: Jeff Evans <email@hidden>
  • Date: Tue, 25 Feb 2014 10:53:08 -0800

I suppose I'm going to look like an idiot, but I'm used to it and would like to ask about the below:

if (_weakIVar)
[_weakIVar someMessage];

Quincy says " some people don’t notice the logical flaw" in this code. I am one of those, and I'm interested in knowing what the flaw is. Is it that the 0 test requires the code to remember to set the var to 0 when released?


Jeff


On Feb 25, 2014, at 10:33 AM, Quincey Morris wrote:

On Feb 25, 2014, at 07:23 , David Hoerl <email@hidden> wrote:

My understanding from reading previous posts here was that if I use a weak object as a message receiver, that essentially that object is retained when the message is sent, and unretained when it returns - thus insuring that the object is kept alive while used within the method.

Did I misunderstand this - can an object "disappear" in the middle of a method?

No, you understand correctly. It can’t disappear.

If the answer is no, then why the warning?

IMO, it’s a spurious attempt at unnecessary handholding.

Aside from a (perfectly valid) programming pattern where you intentionally send messages to objects in variables that might be nil (and that includes strong as well as weak variables), then the rationale is that sending the message via a weak variable *suggests* that you’re expecting it to be non-nil — if you were expecting it to be nil, why send the message? — and therefore it might fail to have the desired effect.

This is a terribly weak rationale, at best, but the warning is there, for those who wish to use it. (One possibly valid use might be to turn it on temporarily, do a build, and use the warnings to check if in fact there are cases where you forgot to deal with the case where the variable is unexpectedly nil.)

A sister warning, "CLANG_WARN_OBJC_REPEATED_USE_OF_WEAK", warns on repeated use of a weak object within a method.

This one is a bit more useful. Presumably it catches code like this:

if (_weakIVar)
[_weakIVar someMessage];

in which some people don’t notice the logical flaw.



If this email is spam, report it to www.OnlyMyEmail.com
Do not post admin requests to the list. They will be ignored. Xcode-users mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
  • Follow-Ups:
    • Re: What does the CLANG_WARN_OBJC_RECEIVER_WEAK warning protect us from?
      • From: Quincey Morris <email@hidden>
References: 
 >What does the CLANG_WARN_OBJC_RECEIVER_WEAK warning protect us from? (From: David Hoerl <email@hidden>)
 >Re: What does the CLANG_WARN_OBJC_RECEIVER_WEAK warning protect us from? (From: Quincey Morris <email@hidden>)

  • Prev by Date: Re: What does the CLANG_WARN_OBJC_RECEIVER_WEAK warning protect us from?
  • Next by Date: Re: What does the CLANG_WARN_OBJC_RECEIVER_WEAK warning protect us from?
  • Previous by thread: Re: What does the CLANG_WARN_OBJC_RECEIVER_WEAK warning protect us from?
  • Next by thread: Re: What does the CLANG_WARN_OBJC_RECEIVER_WEAK warning protect us from?
  • Index(es):
    • Date
    • Thread