Professional Web Applications Themes

Objc Nubie: Errors Errors Errors - Mac Programming

I've been toying with learning ObjC/Cocoa for sometime and finally got over the hump of "brackets vs. braces" but am wondering why my little test throws so many warnings when run: // gcc FileTest.m -o FileTest -ObjC -framework Cocoa #include <Cocoa/Cocoa.h> #include <Foundation/Foundation.h> int main( void ) { NSString * str = [ [NSStringalloc] initWithString:"~/samplefromcocoa.txt" ]; NSString * full = [ NSString stringWithString: [str stringByExpandingTildeInPath ] ]; if( [ [NSFileManager defaultManager] fileExistsAtPath:full ] ) { NSLog( "File % exists", str ); } else { NSString * contents = [ [NSString alloc] initWithString:"Created by FileTest.m" ]; BOOL didWrite = [contents writeToFile:full ...

  1. #1

    Default Objc Nubie: Errors Errors Errors

    I've been toying with learning ObjC/Cocoa for sometime and finally got
    over the hump of "brackets vs. braces" but am wondering why my little
    test throws so many warnings when run:

    // gcc FileTest.m -o FileTest -ObjC -framework Cocoa

    #include <Cocoa/Cocoa.h>
    #include <Foundation/Foundation.h>

    int main( void ) {
    NSString * str = [ [NSStringalloc]
    initWithString:"~/samplefromcocoa.txt" ];

    NSString * full = [ NSString stringWithString:
    [str stringByExpandingTildeInPath ] ];

    if( [ [NSFileManager defaultManager] fileExistsAtPath:full ] ) {
    NSLog( "File % exists", str );
    } else {
    NSString * contents = [ [NSString alloc]
    initWithString:"Created by FileTest.m" ];
    BOOL didWrite =
    [contents writeToFile:full atomically:NO];

    if( didWrite ) {
    NSLog( "Creating File %", str );
    } else {
    NSLog( "Unable to create file %", str );
    }
    }

    return 0;
    }

    when run and it has to write the string to disk>

    2004-02-11 08:27:03.882 FileTest[2082] *** _NSAutoreleaseNoPool():
    Object 0x50d6f0 of class NSCFString autoreleased with no pool in place -
    just leaking
    2004-02-11 08:27:03.885 FileTest[2082] *** _NSAutoreleaseNoPool():
    Object 0x50dc80 of class NSPathStore2 autoreleased with no pool in place
    - just leaking
    2004-02-11 08:27:03.887 FileTest[2082] *** _NSAutoreleaseNoPool():
    Object 0x50dc10 of class NSPathStore2 autoreleased with no pool in place
    - just leaking
    2004-02-11 08:27:03.889 FileTest[2082] *** _NSAutoreleaseNoPool():
    Object 0x50f840 of class NSCFString autoreleased with no pool in place -
    just leaking
    2004-02-11 08:27:03.892 FileTest[2082] *** _NSAutoreleaseNoPool():
    Object 0x50faf0 of class NSURL autoreleased with no pool in place - just
    leaking
    2004-02-11 08:27:03.900 FileTest[2082] *** _NSAutoreleaseNoPool():
    Object 0xa019651c of class NSCFString autoreleased with no pool in place
    - just leaking
    2004-02-11 08:27:03.902 FileTest[2082] *** _NSAutoreleaseNoPool():
    Object 0x50f840 of class NSCFString autoreleased with no pool in place -
    just leaking
    2004-02-11 08:27:03.906 FileTest[2082] Creating File ~/samplefromcocoa.txt

    so can anyone give me a clue?

    Thanks in advance!

    John Guest

  2. #2

    Default Re: Objc Nubie: Errors Errors Errors

    In article <hcqWb.54958$nyroc.rr.com>,
    net says... 
    [snip code]
     
     

    The compiler is giving out lots of clues! The warningsa are telling you
    that you don't have an autorelease pool. To fix this put this line right
    at the top of main():

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    and this one just before main() returns:

    [pool release];
    James Guest

  3. #3

    Default Re: Objc Nubie: Errors Errors Errors

    In article <hcqWb.54958$nyroc.rr.com>,
    John Smith <net> wrote:
     

    Given that you get this error seven times-- maybe it'd be worthwhile
    reading up on NSAutoreleasePool to see if you can make sense of it. :-)

    Seriously, the semantics of retain/release and autorelease are something
    you're going to have to get a good handle on if you're going to be
    writing ObjC on Mac OS X, and a full explanation is really more than I
    can give in a Usenet post. The short answer is that you need to create
    an NSAutoreleasePool before you call methods like +[NSString
    stringWithString]. But you really need to learn why that's the case,
    and that's where reading up a bit will be worth the effort.

    --
    Tom "Tom" Harrington
    Macaroni, Automated System Maintenance for Mac OS X.
    Version 2.0: Delocalize, Repair Permissions, lots more.
    See http://www.atomicbird.com/
    Tom Guest

  4. #4

    Default Re: Objc Nubie: Errors Errors Errors

    John Smith <net> wrote:
     

    [..]
     
    [..] 

    Cocoa assumes an NSAutoreleasePool exists in the current runloop. So put
    one there. It's really easy.

    Convenience class methods, such as +stringWithString:, add their result
    to the autorelease pool, so as to make memory management more...
    convenient. :-) That's why NSCFString is complaining that there's no
    pool, so it never gets released. Your code is leaking one NSCFString
    (and the other objects in your error log).

    Read up about memory management:
    <http://www.stepwise.com/Articles/Technical/HoldMe.html>
    Paul Guest

  5. #5

    Default Re: Objc Nubie: Errors Errors Errors


    THANK YOU THANK YOU THANK YOU
    Somehow (like I'm an idiot) I missed a few key points in the memory
    management sections. I back delved them after reading Paul's link
    and low and behold, NSAutoReleasePool stated a few times in large
    friendly letters.

    Guess this java bum needs to do more reading than experimenting ;)

    Thanks all!

    Paul Mitchum wrote: 
    >>Read up about memory management:
    >><http://www.stepwise.com/Articles/Technical/HoldMe.html>[/ref][/ref]

    John Guest

  6. #6

    Default Re: Objc Nubie: Errors Errors Errors

    I would just add that if you create a new project in Xcode using the type
    "Cocoa Application", a bunch of work will be done for you. It creates an
    NSApplication object which has a default autorelease pool, so you then don't
    have to create and destroy your own (although you still can for
    finer-grained control -- it is possible to have nested autorelease pools).

    Also (nitpick here), people usually put the pointer symbol with either the
    type or the name:

    NSString *str;
    NSString* str;

    I prefer the latter as I think it makes it more clear that the type is
    "string pointer" but people will argue that. Apple clearly prefers the
    former, and you can see that in parameter definition lists they even insert
    a space:

    theString:(NSString *)str
    HiRez Guest

  7. #7

    Default Re: Objc Nubie: Errors Errors Errors

    In article <ash.giganews.com>,
    HiRez <net> wrote:
     

    Many people prefer the former because semantically, the asterisk groups
    with the variable, not with the type; in "int* a, b" b is an int, not a
    pointer to int.

    Reinder
    Reinder Guest

  8. #8

    Default Re: Objc Nubie: Errors Errors Errors

    In article <wxs.nl>,
    Reinder Verlinde <invalid> wrote:
     

    Yes, but I would submit to you that the syntax you show above, mixing
    multiple types in a single declaration line, is error-prone and potentially
    confusing, and is unnecessary. "int *a, b" is no better IMHO, whereas:

    int* a; (or int *a;)
    int b;

    removes all doubt.
    HiRez Guest

  9. #9

    Default Re: Objc Nubie: Errors Errors Errors

    Reinder Verlinde <invalid> wrote in message news:<wxs.nl>... 
    >
    > Many people prefer the former because semantically, the asterisk groups
    > with the variable, not with the type; in "int* a, b" b is an int, not a
    > pointer to int.[/ref]

    The problem with the second syntax is that it mixes very poorly with
    initializers. For example,

    int *p = NULL;

    looks very much like it's saying that *p = NULL. This is a case where
    semantically, the asterisk groups with the type.

    Andrew
    Andrew Guest

Similar Threads

  1. CF4.5 Errors in logs and Unix 111 errors!
    By PoDd69 in forum Coldfusion Server Administration
    Replies: 0
    Last Post: February 15th, 10:24 AM
  2. ora-600 errors
    By Ryan in forum Oracle Server
    Replies: 3
    Last Post: October 27th, 10:40 AM
  3. Replies: 1
    Last Post: October 7th, 01:51 PM
  4. Pop-up window errors:"translators not loaded due to errors"
    By adam1962 webforumsuser@macromedia.com in forum Macromedia Dreamweaver
    Replies: 2
    Last Post: July 29th, 04:04 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139