Can't Even Get 'Hello World' To Work!

Discussion in 'Mac Programming' started by MadDoc, Oct 30, 2007.

  1. macrumors 6502

    MadDoc

    Joined:
    Apr 25, 2005
    Location:
    UK
    #1
    Hi,

    I am really new to Objective-C and Cocoa and have been teaching myself using "Programming in Objective-C" to good effect using Xcode 2 on Tiger.

    I just upgraded to leopard (and thus Xcode 3) and something has happened and now I can't get any of my programs to run :confused:

    I have been making "Foundation tools" (command line utilities) from the book. This is the code:

    Code:
    #import <Foundation/Foundation.h>
    
    int main (int argc, const char * argv[]) {
        NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
        // insert code here...
        NSLog(@"Hello, World!");
        [pool drain];
        return 0;
    }
    
    It compiles and "runs" successfully but doesn't display the little white command line window printing "Hello, World!". This is the same with using the printf command and if I use the scanf command then the app remains runing (according to Activity Monitor) but is not on the dock or on my screen?

    What is different about this version of Xcode? How come everything stopped working???

    Thanks,

    MadDoc,
     
  2. macrumors 6502

    Joined:
    Mar 31, 2005
    Location:
    London, England
    #2
    Have you opened the Console window? Choose "Console" from the Run menu.

    By the way, why are you draining your autorelease pool, rather than releasing it?
     
  3. macrumors 6502

    Joined:
    Dec 6, 2006
    #3
    Oi Nutter,

    Sorry couldn't resist :)

    drain seems to be default for XCode 3 now. Seems to work even if you aren't using gc.
     
  4. macrumors 6502

    Joined:
    Mar 31, 2005
    Location:
    London, England
    #4
    Haha.

    How is that possible? Surely you have to release the pool, otherwise it will leak.
     
  5. thread starter macrumors 6502

    MadDoc

    Joined:
    Apr 25, 2005
    Location:
    UK
    #5
    Thanks, that seems to work great.

    BTW, is there anyway to enable this by default (the console window always used to display without me having to select console from the Run menu)?

    As for using 'drain' I hadn't been - that was simply the empty foundation tool program :rolleyes:

    Thanks,
     
  6. macrumors 6502

    Joined:
    Mar 31, 2005
    Location:
    London, England
    #6
    The console window is always used for output, it's just that you didn't have it open. I can't find any way to get it to open automatically when you run, so you should just leave it open.

    Anyone know why the template is now using drain? I'm surprised by this. Apple's memory management documentation still uses release - in fact, it doesn't mention drain at all.
     
  7. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #7
    The documentation says it "ultimately calls objc_collect_if_needed". So I'm assuming that function is what cleans up the pool.

    Plus, in this instance, it probably doesn't matter if you don't even release/drain the pool since the process is exiting and the OS will clean up the memory anyways (that is why sometimes dealloc is never called - it is faster for the OS to remove the used memory itself).
     

Share This Page