Objective C, New to @property

Joined
Mar 22, 2010
Messages
2
Reaction score
0
Points
1
Ok I am working on a very simple example to help me understand objective c @property function. I don't understand why if I run the code I keep getting the _NSAutoreleaseNoPool(): Object 0x2040 of class NSCFString autoreleased with no pool in place - just leaking What does this mean anyway, I am leaking memory because I didn't release it? What wasn't released?

I am not new to C/C++. I implemented my own getter and setter with no problem. If I use the @property to generate code and use anything except @property (assign) NSString *name; then I get this error. This assign is a pointer assignment, while the other two, copy, retain are just similar but increase the count for this object.

Please help, as I have been staring at this for awhile now. I left my code for the setter and getter to ask, is this what the code @property generates? If it does I don't know why it is giving me problems. Looking at main, all I see is that I have set my objects with constant NSStrings. Then I output a object's name by writing it to NSLog. Right when this is called I error.

Code:
@interface Person : NSObject
{
	NSString *name;	
}

- (id)init;
@property (retain) NSString *name;
@end

@implementation Person;

-(id)init
{
	if(self = [super init])
	{
		
		name = @"Bob";
		
	}
	return self;
}

@synthesize name;

/*- (NSString *)name
{
	return name;
}*/

/*- (void)setName:(NSString *)newName 
{ 

if (name != newName) //
	{ 
    [name release]; 
    name = [newName retain]; 

	} 
}*/


- (void)dealloc
{
	//class cleanup
	[name release];
	//super cleanup
	[super dealloc];
}


@end

int main(int argc, char *argv[])
{
	Person *bill = [[Person alloc] init];
	Person *bob = [[Person alloc] init];
	
	[bill setName:@"bill"];
	[bob setName:@"bob"];

	NSLog(bob name];

	[bob release];	
	[bill release];
    return NSApplicationMain(argc,  (const char **) argv);
}
 

Raz0rEdge

Well-known member
Staff member
Moderator
Joined
Jul 17, 2009
Messages
15,886
Reaction score
2,246
Points
113
Location
MA
Your Mac's Specs
2022 Mac Studio M1 Max, 2023 M2 MBA
In your main function, you need to create a AutoReleasePool and release it at the end of the function..so add
Code:
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
as the first variable and then before you leave the function, add
Code:
[pool release]

Check out Objective-C Beginner's Guide..

Regards
 
OP
C
Joined
Mar 22, 2010
Messages
2
Reaction score
0
Points
1
Hey, I just wanted to say thanks. I added those two lines of code and it worked. I have seen your link before on Objective C, but I overlooked it. I am working off the Standford slides that are posted on itunes. I haven't paid for the iphone SDK yet so their might be differences, although in the link you gave me it says
When developing Mac Cocoa applications, the auto release pool is setup automatically for you.
That is a little strange, yes I have #import <Cocoa/Cocoa.h> in my main. Hey, it works for now and that is what is important.:Cool:
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top