keyongtech


  keyongtech > mac.programmer.help > 02/2004

 #1  
02-11-04, 01:35 PM
John Smith
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!
 #2  
02-11-04, 01:42 PM
James Weatherley
In article <hcqWb.54958$n62.28942>,
user says...
> 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:
>

[snip code]

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


> so can anyone give me a clue?


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];
 #3  
02-11-04, 05:49 PM
Tom Harrington
In article <hcqWb.54958$n62.28942>,
John Smith <user> wrote:

> 2004-02-11 08:27:03.882 FileTest[2082] *** _NSAutoreleaseNoPool():
> Object 0x50d6f0 of class NSCFString autoreleased with no pool in place -
> just leaking


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.
 #4  
02-12-04, 12:10 AM
Paul Mitchum
John Smith <user> wrote:

> 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 ] ];


[..]

> 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

[..]
>
> so can anyone give me a clue?
>
> Thanks in advance!


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>
 #5  
02-12-04, 05:36 AM
John Smith
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:
 #6  
02-14-04, 11:01 PM
HiRez
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
 #7  
02-15-04, 10:02 AM
Reinder Verlinde
In article <hirez-C81BE2.15012814022004>,
HiRez <hirez> wrote:

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


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
 #8  
02-15-04, 05:45 PM
HiRez
In article <reinder-C2FB45.11024015022004>,
Reinder Verlinde <reinder> 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.


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.
 #9  
02-16-04, 07:30 PM
Andrew Duncan
Reinder Verlinde <reinder> wrote in message news:2004
> In article <hirez-C81BE2.15012814022004>,
> HiRez <hirez> wrote:
> > 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.

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


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
Similar Threads
DCOM Errors / Access denied drives / Removable storage errors

I am Running Windows XP pro SP3 on an Acer Desktop I originally had issues with my original CD-ROM, DVD RW, CD RW Drive. Where it would not read anytype of media...

compile errors for Game The main program is giving me errors projectJS.java

Hello again. I've only included two of the 13 programs for this game. The main program projectJS.java is giving me compile errors and the other program containing private...

Directory Service Referral interface failed errors and Group policy errors

Ok here is the situation...... Windows 2000 SBS server had to be rebooted yesterday due to power failure, when the server came backup nobody could send emails, as soon as...

.Net Framework 2005 Beta 2 causes live TV errors, video errors, guide errors

My Media Center 2004 stopped working recently. I was getting live TV errors, missing video files errors, and guide errors. I tried reinstalling Media Center, reinstalling...

[PEAR] QuickForm javascript errors - need to supress errors when returning to form

I have a QuickForm working correctly. My app is a quiz. The user registers, takes the quiz and is passed back to the form to start again if they don't pass the quiz. I record...


All times are GMT. The time now is 10:07 PM. | Privacy Policy