when should release be called

Joined
May 27, 2012
Messages
5
Reaction score
0
Points
1
I am doing some exercise on the code sample CTPageViewer, I am really puzzled for release function. Take the follow code for an example:

Code:
- (void)loadWithFileName {	

	// .......... some codes
	
	NSString* filePath = [self filePathForFileName:fileName];
	if (filePath == NULL) {
		NSLog(@"%@ could not be opened/found", fileName);
		return;
	}

	// Get document dictionary
	NSDictionary* docDict = [NSDictionary dictionaryWithContentsOfFile:filePath];
	if (docDict == NULL) {
		NSLog(@"%@ contents cannot be processed", filePath);
		return;
	}
	
	// Check that document version info is supported
	NSArray* versionArr = [docDict objectForKey:ASD_VERSION];
	if (![AttributedStringDoc versionIsValid:versionArr] || ![AttributedStringDoc contentIsValid:versionArr]) {
		NSLog(@"%@ could not be opened due to version/content(%@) incompatibility", filePath, versionArr);
		return;
	}

	// .......... some codes deleted	
}


Why the release message does not send to objects such as filePath, docDict and versionArr. And when should I send release message to an object?

I am studying iOS programming in recent weeks, so please forgive me for the simple question.
 
Joined
Aug 13, 2011
Messages
200
Reaction score
7
Points
18
Location
West Sussex
Like your last post, your making some very basic errors in Objective-C, and syntax, and also eliminating some important parts of the code.

Your if statements are checking if an NSString, and a NSDictionary are NULL, neither the NSString or NSDictionary classes can be a value of NULL, but they can be a value of nil.

The NULL value applies to C and Java, but not Objective-C classes.

Your question about releasing objects is a good one, but is not a straightforward one, and can vary a great deal to different classes, for example if you add an Objective-C variable to ann NSArray, you can release that variable as soon as its added to the array, because the array holds a pointer to it, this is the same as UIView classes, they also can be released as soon as they are added to a super view, as the super view retains it.

In short, you can release an object as soon as your finished with it, which is an over simplified explaination, but this can vary with so many different factors.

I can only advise you to check out the book recommendation I made in your last post, also you might consider using ARC for your projects, this will eliminate the need for you to release your variables manually, or alternatively consider using autoreleased variables more frequently in your code, for the same reasons.

Good Luck with it.

Regards Mark
 
OP
C
Joined
May 27, 2012
Messages
5
Reaction score
0
Points
1
Like your last post, your making some very basic errors in Objective-C, and syntax, and also eliminating some important parts of the code.

Your if statements are checking if an NSString, and a NSDictionary are NULL, neither the NSString or NSDictionary classes can be a value of NULL, but they can be a value of nil.

The NULL value applies to C and Java, but not Objective-C classes.

Your question about releasing objects is a good one, but is not a straightforward one, and can vary a great deal to different classes, for example if you add an Objective-C variable to ann NSArray, you can release that variable as soon as its added to the array, because the array holds a pointer to it, this is the same as UIView classes, they also can be released as soon as they are added to a super view, as the super view retains it.

In short, you can release an object as soon as your finished with it, which is an over simplified explaination, but this can vary with so many different factors.

I can only advise you to check out the book recommendation I made in your last post, also you might consider using ARC for your projects, this will eliminate the need for you to release your variables manually, or alternatively consider using autoreleased variables more frequently in your code, for the same reasons.

Good Luck with it.

Regards Mark


Thanks
 

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