11

After updating to XCode 6.3, compiler started giving this warning.

Comparison of address of 'myObject' not equal to null pointer is always true.

Here is my piece of code,

enter image description here

Scratching my head with it, but didn't find any solution or workaround to get rid of this warning.

My question is linked with question over here. But, it can't be solved using answer discussed.

Any help will be welcomed :)

Community
  • 1
  • 1
itsji10dra
  • 4,603
  • 3
  • 39
  • 59
  • can this be suppressed? – Michael Apr 13 '15 at 18:43
  • I was wondering if this particular comparison issue could be suppressed, since it was occurring in a 3rd party submodule we didn't want to modify. Instead we fixed the issue (following the answer from Inder Kumar Rathore) and forked it. – Michael Apr 14 '15 at 22:08
  • 2
    I'm using the same library you are and encountered the same problem. It's a nice class, but sadly does not appear the author is maintaining it. I decided to fork it and fix up some of these warnings myself. You can find my fork here: https://github.com/dannys42/WYPopoverController – Danny Sung May 28 '15 at 22:11
  • @DannySung Thanks for fixing it :) – itsji10dra May 29 '15 at 05:19

3 Answers3

10

Correct way of checking the pointer is

if (anotherInView != nil) {
}

You are comparing address of a variable with the NULL. Every variable has an address you can't have a variable whose address is NULL, you can have a variable whose value is NULL

Also anotherRect != NULL is again not valid. anotherRect will always have a value as it's a struct

Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184
  • That's actually not true. Those bits are written that way to detect whether the symbol exists on older platforms. You're getting this warning because your minimum target is new enough that it always exists. However, if you ever target an older OS and you try to test the value of the pointer itself, you'll get a segfault, because the *address* of the pointer doesn't exist, much less the value of that pointer. (Yes, weak linking is scary.) – dgatwood Jul 01 '15 at 22:06
  • 1
    Correction: In this case, it is a mistake. It is not *always* a mistake, because that construction can be used for the reason I mentioned. I didn't look carefully enough at the code before commenting. :-) – dgatwood Jul 09 '15 at 17:27
1

There are four mistakes here. The first two are that you aren't setting any initial values for the CGRect and UIView variables, so there's no way to detect a failure. Try setting the CGRect to a zero-width, zero-height rect initially if you think that is useful, and set the UIView to nil.

The third and fourth mistakes are that the address of a local variable is never NULL. (Note that this is not true for global variables in libraries, as I mentioned in another comment—the address of an NSString pointer constant might be NULL if the symbol didn't exist on the version of the OS you're running—but for local variables, you're guaranteed an address up to the point where your stack overflows and your app crashes.)

I'm also puzzled by why you're calling a delegate method yourself. Normally, the OS itself calls those methods, so calling them yourself is somewhat atypical usage. To be fair, I've done it on occasion when a delegate method performs a computation that I need somewhere else in the code, and I've also done it when implementing delegates that call other delegates, but in the context of this question, it seemed like a potential mistake.

dgatwood
  • 10,129
  • 1
  • 28
  • 49
1

For the first instruction, a changed to :

 if (!CGRectIsNull(anotherRect)) {

 }
Felipe FMMobile
  • 1,641
  • 20
  • 17