Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > Mac OS X Development Forums > Objective-C, Python, Ruby Development

Reply
 
LinkBack Thread Tools Display Modes
Old 03-19-2010, 04:03 AM   #1 (permalink)
Registered Member
 
Join Date: Dec 2009
Posts: 97
Question Memory Management in Custom Class

Hello,

by doing more and more Objective-C I somehow get more and more confused about memory management. I have read the according document but what I need are some best practice examples. Here I have a small example class and a couple of questions.

Are there any other helpful guides / documents explaining how to write custom classes?
Code:
@interface MyClass : NSObject {
	NSString *name;
	CoolObject *coolObject;
}

@property (readonly, retain) NSString *name;
@property (readwrite, retain) CoolObject *coolObject;

- (id)initWithName:(NSString *)theName;
- (void)doWhatever;

@end
@implementation MyClass

@synthesize name, coolObject;

// custom initializer
- (id)initWithName:(NSString *)theName {
	if (self = [super init]) {
		name = theName;
		coolObject = [[CoolObject alloc] init];
	}
	return self;
}

// overwrite setter
- (void)setName:(NSString *)theName {
	if (theName != nil && [theName length] > 0) {
		name = theName;
	}
}

// overwrite setter
- (void)setCoolObject:(CoolObject *)theObject {
	if (theObject != nil) {
		coolObject = theObject;
	}
}

- (void)doWhatever {
    self.coolObject = [[CoolObject alloc] init];
}

- (void)dealloc {
	[name release];
	[coolObject release];
	[super dealloc];
}
@end
1. should a NSString property be assigned, retained or copied? What are best practices? Or when should I chose which?

2. in the custom initializer:
Is there a leak assigning the object?
Would this be better?

CoolObject *tmp = [[CoolObject alloc] init];
coolObject = tmp;
[tmp release];

What about the method doWhatever? Does this leak an object?
should it rather be:

[coolObject release];
CoolObject *tmp = [[CoolObject alloc] init];
self.coolObject = tmp;
[tmp release];

3. overwritten setters: is there a call of [name release] / [coolObject release] missing before the assignment?

4. dealloc: is it correct that

coolObject = nil;

is similiar to

[coolObject release];
coolObject = nil;

Thanks!
SteveMobs is offline   Reply With Quote
Old 03-19-2010, 04:55 AM   #2 (permalink)
Registered Member
 
Join Date: Dec 2009
Posts: 97
Default

oh, I just found this thread which perfectly covers most of my questions!

http://www.iphonedevsdk.com/forum/ip...es-newbie.html

sorry for posting too quickly
SteveMobs is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On



» Advertisements
» Online Users: 272
21 members and 251 guests
ADY, AragornSG, Bertrand21, Dani77, Dattee, fkmtc, HDshot, HemiMG, iDifferent, JasonR, jimbo, macquitzon216, mer10, prchn4christ, Rudy, sacha1996, sneaky, spiderguy84, Sunny46, theone8one
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,885
Threads: 89,230
Posts: 380,767
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:10 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0