weird compiler warnings/errors in Xcode 2.0

Member
Posts: 208
Joined: 2005.04
Post: #1
I just upgraded to Tiger last night (from Panther), and now many of my Cocoa projects produce compiler warnings which never appeared before. In both of the following examples, selectedDevice is an io_object_t.

"warning: comparison between pointer and integer"
is produced by the following code:
Code:
if (selectedDevice != NULL) { //do stuff }

"warning: assignment makes integer from pointer without a cast"
is produced by the following code:
Code:
selectedDevice = NULL;


What do you think the problem is? Is it my code, Xcode 2.0, or GCC4?

Thanks in advance,

Andrew G.
Quote this message in a reply
Member
Posts: 208
Joined: 2005.04
Post: #2
Here are 2 more examples of how NULLs aren't being treated correctly (IMHO) by the compiler:

"warning: passing argument 4 of 'IORegistryEntryCreateCFProperty' makes integer from pointer without a cast"
Code:
CFNumberRef locationID;
locationID = IORegistryEntryCreateCFProperty( interfaceToCheckLocationOf,
                        CFSTR(kIOHIDLocationIDKey),
                        kCFAllocatorDefault,
                        NULL);

"warning: comparison between pointer and integer"
Code:
if (IOIteratorNext(deviceInterfaceIterator) == NULL) { //do stuff }
Quote this message in a reply
Member
Posts: 41
Joined: 2005.02
Post: #3
You said selectedDevice is an io_object_t - not an io_object_t pointer. As far as I can tell, pointers really haven't changed at all in GCC 4, I think it's that you are treating the io_object_t as a pointer when it's really just some sort of reference number. Perhaps replace all the NULLs with 0's or read the documentation on the io_object_t and see if there's a default or NULL value for it.

You can always run gcc_select as root (or with sudo) and change back to 3.3 to see if it's just 4.0 that brings out this problem. I would try compling with -Wall (all warnings) under both 3.3 and 4.0 to see if one errors when the other one just warns.

~ Travis
Quote this message in a reply
Member
Posts: 41
Joined: 2005.02
Post: #4
Code:
CFNumberRef locationID;
locationID = IORegistryEntryCreateCFProperty( interfaceToCheckLocationOf,
                        CFSTR(kIOHIDLocationIDKey),
                        kCFAllocatorDefault,
                        NULL);
Read the docs - an int is expected, not a NULL, for IORegistryEntryCreateCFProperty(), so pass a zero. The examples even pass an int. Smile

~ Travis
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #5
These are problems with your code.

NULL is a pointer. Don't assign it to non-pointer variables. io_object_t ain't a pointer type:

Code:
typedef unsigned int        __darwin_natural_t;
typedef __darwin_natural_t    natural_t;
typedef natural_t mach_port_name_t;
typedef mach_port_name_t         mach_port_t;
typedef mach_port_t    io_object_t;

-[NSMutableAttributedString setAttributes:range:] doesn't return anything:

Code:
- (void)setAttributes:(NSDictionary *)attributes range:(NSRange)aRange
Quote this message in a reply
Member
Posts: 41
Joined: 2005.02
Post: #6
Code:
typedef unsigned int        __darwin_natural_t;
typedef __darwin_natural_t    natural_t;
typedef natural_t mach_port_name_t;
typedef mach_port_name_t         mach_port_t;
typedef mach_port_t    io_object_t;

Man, that obnious set of typedefs. I'm glad OSC looked it up and not me.

Grin

~ Travis
Quote this message in a reply
Member
Posts: 208
Joined: 2005.04
Post: #7
Thanks guys! Everything works now Smile

I always figured "NULL" was a macro that simple pasted "0" into my code. Now I know better Wink
Quote this message in a reply
Member
Posts: 208
Joined: 2005.04
Post: #8
That stuff about centering a paragraph (which I deleted from my original post because I realized my own stupid mistake after posting) is still causing trouble. I've changed the code to this:

Code:
NSMutableParagraphStyle *centeredParagraphStyle = [[[NSMutableParagraphStyle alloc] init] autorelease];

[centeredParagraphStyle setAlignment:NSCenterTextAlignment];
    
[string_attributes setObject:centeredParagraphStyle
                  forKey:NSParagraphStyleAttributeName];

and it compiles fine... but now I get a runtime error:

*** -[NSParagraphStyle count]: selector not recognized [self = 0x4770250]

could this be a bug in AppKit?
Quote this message in a reply
Member
Posts: 41
Joined: 2005.02
Post: #9
Andrew Wrote:I always figured "NULL" was a macro that simple pasted "0" into my code. Now I know better Wink

Often, in pratice, it is; I wouldn't consider it the same thing though. NULL could be any internal way for marking that the pointer as pointing to nothing. Just as giving a pointer the value zero it not really a good idea, though often it works.

~ Travis
Quote this message in a reply
Puzzler183
Unregistered
 
Post: #10
Uhm, actually, I'm fairly certain that according to the standard, if you cast NULL to an int it must be zero. I don't have a copy to look it up in but yeah, NULL must basically be (void *) 0.

Still no reason to confuse the two.
Quote this message in a reply
Member
Posts: 208
Joined: 2005.04
Post: #11
with regards to the NULL stuff, I found the definitions:

in /usr/include/stdio.h:

#define NULL __DARWIN_NULL

and in /usr/include/sys/_types.h:

#define __DARWIN_NULL ((void *)0)
Quote this message in a reply
Member
Posts: 41
Joined: 2005.02
Post: #12
Andrew Wrote:with regards to the NULL stuff, I found the definitions:

in /usr/include/stdio.h:

#define NULL __DARWIN_NULL

and in /usr/include/sys/_types.h:

#define __DARWIN_NULL ((void *)0)

If you look more closely you'll see that the ((void *) 0) only applies sometimes. Otherwise you have a __null or few other things such as 0 or 0L. It's a rather confusing set of ifdefs really.

I never found what __null was, it's not in any file on my system. So it's an internal GCC thing I assume.

~ Travis
Quote this message in a reply
Moderator
Posts: 3,579
Joined: 2003.06
Post: #13
You can command-double-click on any symbol in Xcode and the file where it's defined comes up in another window. In the case of NULL you wind up with two possible paths, both of which wind up with NULL being defined as ((void *)0). Interestingly, the definition in stddef undefines whatever is defined for NULL in stdio. It still becomes ((void *)0) however.

[Edit] BTW, you can then get the full path to that file by right-click->Get Info in the text view.
Quote this message in a reply
Member
Posts: 208
Joined: 2005.04
Post: #14
iefan Wrote:If you look more closely you'll see that the ((void *) 0) only applies sometimes. Otherwise you have a __null or few other things such as 0 or 0L. It's a rather confusing set of ifdefs really.

I never found what __null was, it's not in any file on my system. So it's an internal GCC thing I assume.

Here's the full set of def's for __DARWIN_NULL (nicely indented):

Code:
#ifdef __cplusplus
  #ifdef __GNUG__
    #define __DARWIN_NULL __null
  #else /* ! __GNUG__ */
    #ifdef __LP64__
      #define __DARWIN_NULL (0L)
    #else /* !__LP64__ */
      #define __DARWIN_NULL 0
    #endif /* __LP64__ */
  #endif /* __GNUG__ */
#else /* ! __cplusplus */
  #define __DARWIN_NULL ((void *)0)
#endif /* __cplusplus */

So, as long as you're not using C++, NULL should always be ((void *)0) on Mac OS X (well... on Tiger anyway).
Quote this message in a reply
VolganPoet
Unregistered
 
Post: #15
Andrew, you might try setting a break point on the call. Since count isn't a selector defined for NSParagraphStyle, you'll need to make one to break on.

Code:
@interface NSParagraphStyle(DBGBP)
- (unsigned) count;
@end

@implementation NSParagraphStyle(DBGBP)
- (unsigned) count
{
    [NSException raise:@"Invalid Selector"
        format:@"NSParagraphStyle does not respond to count"];
}
@end

Set a break point in the new count method, run the debugger, then you'll see where the call came from and hopefully what the bug is. Oh, and delete the category when you're finished.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Weird Compiler WARNING... help? Elphaba 1 2,284 Aug 10, 2009 10:51 PM
Last Post: Elphaba
  Curious Compiler Issue? WhatMeWorry 1 2,517 Jan 16, 2007 03:52 PM
Last Post: OneSadCookie
  Weird errors with std::map ermitgilsukaru 8 4,131 Nov 30, 2006 04:10 AM
Last Post: DoG
  Intel Mac -> Many Warnings (deprecated functions) dave05 3 4,661 Sep 6, 2006 03:19 PM
Last Post: aarku
  compiler (pre-processing?) error of SDL project WhatMeWorry 2 2,971 Oct 14, 2005 09:09 PM
Last Post: WhatMeWorry