New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus Advice and insight from world-class Apple enthusiasts Exclusive access to members-only contests, giveaways and deals

Join today!

 
Start a Discussion
 

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
OS X - Development and Darwin Discussion and questions about development for Mac OS X.

Objective C, New to @property


Post Reply New Thread Subscribe

 
Thread Tools
C4tSn4cks

 
Member Since: Mar 22, 2010
Posts: 2
C4tSn4cks is on a distinguished road

C4tSn4cks is offline
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);
}
QUOTE Thanks
Raz0rEdge

 
Raz0rEdge's Avatar
 
Member Since: Jul 17, 2009
Location: MA
Posts: 7,729
Raz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant futureRaz0rEdge has a brilliant future
Mac Specs: 27" i7 iMac, 24" iMac, 13" Macbook Air, iPhone 5 & 5S, iPod Nano 7th Gen, iPad 2 16GB WiFi, iPad 3

Raz0rEdge is offline
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
QUOTE Thanks
C4tSn4cks

 
Member Since: Mar 22, 2010
Posts: 2
C4tSn4cks is on a distinguished road

C4tSn4cks is offline
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
Quote:
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.
QUOTE Thanks

Post Reply New Thread Subscribe


« Using an NSArrayController as an attribute. | Setting Deployment Target »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread
Thread Starter
Forum
Replies
Last Post
why classes and objects are similar in objective c harsha973 OS X - Development and Darwin 2 05-11-2010 08:12 PM
dynamic mwmory allocation in objective c drewangel OS X - Development and Darwin 1 03-17-2010 02:45 AM
Has to be an easier way to deal with Time in Objective C fajhetti OS X - Development and Darwin 2 02-03-2010 12:43 PM
Objective C Books that don't need C knowledge J-Mak OS X - Development and Darwin 4 10-27-2009 01:31 AM
question about objective C memory allocation thuffir OS X - Development and Darwin 3 11-18-2008 11:30 PM

All times are GMT -4. The time now is 09:56 AM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
X

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?