PDA

View Full Version : Can't Even Get 'Hello World' To Work!




MadDoc
Oct 30, 2007, 06:27 AM
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:

#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,



Nutter
Oct 30, 2007, 06:33 AM
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?

garethlewis2
Oct 30, 2007, 06:42 AM
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.

Nutter
Oct 30, 2007, 06:59 AM
Haha.

How is that possible? Surely you have to release the pool, otherwise it will leak.

MadDoc
Oct 30, 2007, 07:12 AM
Have you opened the Console window? Choose "Console" from the Run menu.

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,

Nutter
Oct 30, 2007, 07:50 AM
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.

kainjow
Oct 30, 2007, 07:56 AM
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.

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).